如何捕获 Blazor WebAssembly 中的 EditForm 组件是否“脏”

Fab*_*nus 5 blazor blazor-client-side

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

Isa*_*aac 6

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

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));


Run Code Online (Sandbox Code Playgroud)

  • 这个问题还有一个方面——执行更新后如何重置 IsModified 标志? (6认同)
  • 您可以使用“EditContext.MarkAsUnmodified()”来重置状态 (3认同)