Accessing Async Property in Razor page

asked7 years ago
last updated7 years ago
viewed10.7k times
Up Vote12Down Vote

I am trying to bring up a simple .NET Core 2.0 Web Application with Razor pages. The page is connected to an equally simple Core 2.0 Web API. I have a simple class:

public class About : PageModel
{
    private ServiceProxy serviceProxy;

    public About(ServiceProxy serviceProxy)
    {
        this.serviceProxy = serviceProxy;
    }

    public IEnumerable<ProductViewModel> Values { get; set; }

    public async void OnGetAsync()
    {
        this.Values = await this.serviceProxy.GetValuesAsync();
    }
}

And the page is also simple:

@page
@model About
@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@Model.Message</h3>
@foreach (var product in @Model.Values)
{
    <p>@product.Name</p>
}

However, the page displays before the OnGetAsync() method can populate the 'Values' list. This seems like such a common operation, but I can't find any discussion of it (I've also tried iterating over an async 'GetValues()' method).

How are CSHTML pages supposed to interact with a Web API that may take a few seconds to return results?