blazor - 如果 Blazor webassembly 中的 EditForm 组件为 "dirty",如何捕获

标签 blazor blazor-client-side

对于 Blazor Webassembly 中的 EditForm,是否存在与 Angular 中的脏表单概念等效的概念?我想显示一个文本“您已经进行了更改。任何未保存的更改都将丢失!”向用户表明某些内容尚未保存,离开前应点击提交按钮。

最佳答案

是的,有,但我们不使用脏话,我们使用修改或未修改。

EditContext 类提供以下内容:

     /// <summary>
        /// Determines whether any of the fields in this <see cref="EditContext"/> have been modified.
        /// </summary>
        /// <returns>True if any of the fields in this <see cref="EditContext"/> have been modified; otherwise false.</returns>
        public bool IsModified()
        {
            // If necessary, we could consider caching the overall "is modified" state and only recomputing
            // when there's a call to NotifyFieldModified/NotifyFieldUnmodified
            foreach (var state in _fieldStates)
            {
                if (state.Value.IsModified)
                {
                    return true;
                }
            }

        return false;
        }


    /// <summary>
        /// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
        /// </summary>
        /// <returns>True if the field has been modified; otherwise false.</returns>
        public bool IsModified(in FieldIdentifier fieldIdentifier)
            => _fieldStates.TryGetValue(fieldIdentifier, out var state)
            ? state.IsModified
            : false;

        /// <summary>
        /// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
        /// </summary>
        /// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
        /// <returns>True if the field has been modified; otherwise false.</returns>
        public bool IsModified(Expression<Func<object>> accessor)
            => IsModified(FieldIdentifier.Create(accessor));


关于blazor - 如果 Blazor webassembly 中的 EditForm 组件为 "dirty",如何捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60328796/

相关文章:

c# - 是否可以在执行代码之前加载服务器端 Blazor 页面 HTML?

azure - 在 Azure AD B2C 中取消新用户注册重定向到站点主页,产生 "AuthorizationFailed"错误

async-await - Blazor WebAssembly : How to get UI to update during long running, 非异步进程

authentication - 如何使用 Identity Server 授权 Blazor WebAssembly SPA 应用程序

c# - 如何使用 Blazor Server 对用户进行身份验证

authentication - 如何在 Blazor WebAssembly 中实现 OIDC 身份验证?

forms - 在对话框中使用 FluentValidation 和 MudBlazor 进行表单验证

Blazor WebAssembly 编译速度慢(最多 30 秒)

blazor - 无法覆盖客户端blazor中的OnAfterRenderAsync

Blazor 如何创建动态表单