gpt4 book ai didi

c# - 为什么 Blazor UI 组件在删除事件后没有更新?

转载 作者:行者123 更新时间:2023-12-03 13:50:25 26 4
gpt4 key购买 nike

为什么 Blazor 用户界面 删除事件后不更新:

我的组件:

<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="@(async () => await DeleteTechnique(trainingTechnique.Id))">
Delete
</button>

</td>
</tr>
}
</tbody>
</table>

我后面的组件代码:

public class TrainingTechniqueViewPageBase : ComponentBase
{
public List<TrainingTechniqueView> TrainingTechniques { get; set; }
[Inject]
public ITrainingTechniqueConsumer TrainingTechniqueConsumer { get; set; }

protected TrainingTechniqueForm TrainingTechniqueForm { get; set; }
protected override async Task OnInitializedAsync()
{
TrainingTechniques = (await TrainingTechniqueConsumer.GetTechniques()).ToList();
}

public async void TrainingTechniqueForm_OnSave()
{
TrainingTechniques = (await TrainingTechniqueConsumer.GetTechniques()).ToList();
StateHasChanged();
}

protected void AddTrainingTechnique()
{
TrainingTechniqueForm.Show();
}

protected async Task DeleteTechnique(int id)
{
await (TrainingTechniqueConsumer.DeleteTrainingTechnique(id));
this.StateHasChanged();

}
}
}

删除方法:

public async Task DeleteTrainingTechnique(int id)
{
await _httpClient.DeleteAsync($"training/trainingtechniques/{id}");
}

最佳答案

解决方案 1 :从源重新加载 TrainingTechniques 列表

private async Task Delete(int id)
{
await TrainingTechniqueConsumer.DeleteTrainingTechnique(id);
TrainingTechniques = (await TrainingTechniqueConsumer.GetTechniques()).ToList();
}

解决方案 2 :从列表中删除项目:
<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>

和删除方法:

private async Task Delete(int id)
{
TrainingTechniques.RemoveAll(x => x.Id == Id);
await TrainingTechniqueConsumer.DeleteTrainingTechnique(id);
}

关于c# - 为什么 Blazor UI 组件在删除事件后没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61091650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com