'How to make my blazor web assembly list update
I have an MVC project with blazor web assembly razor components embedded in the mvc cshtml pages.
One razor component contains the following items:
@foreach (Thing thing in things)
{
<div>@(thing.Name)</div>
}
<EditForm Model="@(things)">
<button type="button" @onclick="@(GetThings)">Get Things</button>
</EditForm>
@code
{
[Parameter]
public AllThings allthings{ get; set; }
private List<Thing> things{ get; set; }
protected override async Task OnInitializedAsync()
{
things= allthings.Things;
}
public async void GetThings()
{
IMyHttpClient myhttpclient = new MyHttpClient();
List<Thing> newthings = await client.GoGetStuff("things/new");
things = newthings;
}
}
The service, MyHttpClient, is returning a new list okay but the rendered list on the page just disappears.
I have tried iterating through the initial things list in the GetThings method and just deleting a few items then adding items from the newthings list, the old items disappear but the new items do not render.
How can I get the list to update please?
Solution 1:[1]
Try using
public async Task GetThings()
instead of
public async void GetThings()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
