Why the Blazor UI component doesn't update after delete event?
Solution 1: Reload the TrainingTechniques list from the source
private async Task Delete(int id)
{
await TrainingTechniqueConsumer.DeleteTrainingTechnique(id);
TrainingTechniques = (await TrainingTechniqueConsumer.GetTechniques()).ToList();
}
Solution 2: Remove the item form the list:
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Example</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var trainingTechnique in TrainingTechniques)
{
<tr>
<td>@trainingTechnique.Id</td>
<td>@trainingTechnique.Name</td>
<td>@trainingTechnique.Example</td>
<td>
<button type="button" class="btn btn-danger" @onclick="@(() => DeleteTechnique(trainingTechnique.Id))">Delete</button>
</td>
</tr>
}
</tbody>
</table>
And the Delete method:
private async Task Delete(int id)
{
TrainingTechniques.RemoveAll(x => x.Id == Id);
await TrainingTechniqueConsumer.DeleteTrainingTechnique(id);
}
Remove the item from the list that has the binding to the UI.
protected async Task DeleteTechnique(int id)
{
await (TrainingTechniqueConsumer.DeleteTrainingTechnique(id));
TrainingTechniques.Remove(TrainingTechniques.Single(x => x.Id == id));
}
I think it is better to pass the item it self instead of an Id. For example the delete could be:
protected async Task DeleteTechnique(TrainingTechniqueView corpse)
{
await (TrainingTechniqueConsumer.DeleteTrainingTechnique(corpse.Id));
TrainingTechniques.Remove(corpse);
}