I have a Layout
with a @RenderBody
section and an index
page. My index page has a long running process and I want it renders the view without waiting for DoSomeAsyncStuff
. The following code looks close to what I want but the problem is with my model that it's properties are null when pass to view:
public ActionResult Index()
{
MyModel model = new MyModel();
Task.Run(() => DoSomeAsyncStuff(model));
return View(model);
}
private async void DoSomeAsyncStuff(MyModel model)
{
await Task.Delay(20000);
model.Name = "Something";
//Assigning other model properties
}
Here in my view I get NullReferenceException
and Value cannot be null
errors and certainly it is because my model's properties are not still filled in the DoSomeAsyncStuff
method:
<table>
<tr>
<th colspan="3">
@Model.Products.Select(c => c.Date).FirstOrDefault()
</th>
</tr>
@foreach (var item in Model.Products)
{
<tr>
<td>
@item.Title
</td>
<td>
@item.Price
</td>
</tr>
}
</table>