You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor WebAssembly弹窗取消后UI不更新问题求助

解决方案

直接针对问题给出修复步骤和代码调整:

  1. 修正方法名拼写错误
    你在对话框取消逻辑里调用的ResetItemOriginalValues是拼写错误,正确方法名应为ResetItemToOriginalValues(缺失了To),这会导致重置逻辑根本没执行。

  2. 强制退出表格行编辑状态
    点击提交按钮后,MudTable仍处于行编辑模式,仅重置数据不会自动退出编辑状态,需要手动触发表格取消编辑:

    • 给MudTable添加引用:
      <MudTable @ref="_mudTable"
                Items="@Items"
                @bind-SelectedItem="_selectedItem"
                CanCancelEdit="true"
                OnCommitEditClick="@(() => ItemChangeCommitted())"
                RowEditPreview="BackupItem"
                RowEditCancel="ResetItemToOriginalValues"
      />
      
    • 在代码中声明表格引用:
      private MudTable<YourItemType>? _mudTable;
      
  3. 确保状态更新在UI线程执行
    对话框的回调可能脱离Blazor同步上下文,需用InvokeAsync包裹状态更新操作,保证UI正确刷新。

修改后的完整ItemChangeCommitted方法:

private async Task ItemChangeCommitted()
{
    var result = await DialogService.ShowMessageBox(
        "Warning", 
        "Your changes may have consequences, are you sure you want to proceed?", 
        yesText:"Yes", cancelText:"Cancel");
    
    if (result == null || !result.Value)
    {
        // 调用正确的重置方法
        ResetItemToOriginalValues(_selectedItem);
        // 强制退出表格编辑状态
        _mudTable?.CancelEdit();
        // 在UI线程触发状态更新
        await InvokeAsync(StateHasChanged);
        return;
    }
    
    await HttpClient.PutAsJsonAsync(ApiRoutes.UpdateItem(_selectedItem));
    await InvokeAsync(StateHasChanged);
}

另外,完善ResetItemToOriginalValues的类型校验,确保数据重置逻辑可靠:

private void ResetItemToOriginalValues(object? item)
{
    if (item is YourItemType currentItem && _itemBeforeEdit != null)
    {
        currentItem.Name = _itemBeforeEdit.Name;
        // 其他需要重置的属性请在此补充
    }
}

以上调整后,无论是行内编辑取消还是对话框取消操作,都会正确重置数据并刷新UI。

内容的提问来源于stack exchange,提问作者PalBo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 02:17:56