我想在删除或创建新产品时刷新表组件。我尝试呼叫await InvokeAsync(StateHasChanged());
或StateHasChanged()
但是Table组件不刷新它的数据,我必须手动点击刷新按钮才能看到的变化
public async Task HandleValidSubmit()
{
ReceivedProduct = new MyBlazorApp.Models.Product();
using (var httpClient = new HttpClient())
{
StringContent content = new StringContent(JsonConvert.SerializeObject(ProductData), Encoding.UTF8, "application/json");
using (var response = await httpClient.PostAsync("https://localhost:7104/api/Products", content))
{
string apiResponse = await response.Content.ReadAsStringAsync();
ReceivedProduct = JsonConvert.DeserializeObject<MyBlazorApp.Models.Product>(apiResponse);
}
}
FormSubmitMessage = "Product Created";
await InvokeAsync(StateHasChanged);
}
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
await InvokeAsync(StateHasChanged);
}
在调用StaeHasChanged((的两种情况下,它都是不必要的——它将在事件处理程序之后被调用。
但是表组件不刷新其数据,
您需要重新加载数据。大致:
protected override async Task OnInitializedAsync()
{
await LoadTable();
}
....
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
//await InvokeAsync(StateHasChanged);
await LoadTable();
}