我正在使用MudBlazor
并且有一个简单的EditForm
. 我无法弄清楚为什么表单上的任何内容都没有更改时的EditContext.IsModified
值。只需单击表单上的按钮即可将值从更改为。TRUE
Cancel
IsModified
False
True
@if (modelUserDetails != null) {
<EditForm EditContext="editContext" OnValidSubmit="@SaveChanges">
<MudTextField @bind-Value="modelUserDetails.FirstName" For="@(() => modelUserDetails.FirstName)" Label="First Name" AutoFocus="true" MaxLength="30" Counter="30" Immediate="true" />
<MudTextField @bind-Value="modelUserDetails.LastName" For="@(() => modelUserDetails.LastName)" Label="Last Name" MaxLength="30" Counter="30" Immediate="true" />
<MudButton ButtonType="ButtonType.Button" OnClick="CancelChanges" DisableElevation="true">Cancel</MudButton>
...
</EditForm>
}
.razor 代码文件
private EditUserDTO? modelUserDetails { get; set; }
private EditContext? editContext { get; set; }
protected override async Task OnInitializedAsync() {
modelUserDetails = await UserRepository.GetSingleUser(id);
editContext = new EditContext(modelUserDetails);
editContext.MarkAsUnmodified();
Console.WriteLine($"IsModified: {editContext.IsModified()}"); // False at this point
}
private async Task CancelChanges() {
Console.WriteLine($"IsModified: {editContext.IsModified()}"); // True at this point
...
}
上面的代码是一个缩减版。我只是希望该IsModified
值仅在用户更改了True
任何值时才存在。MudTextField
这里出了什么问题?
我花了几个小时试图解决这个问题;尝试不同的方法,在线搜索,但我没有成功解决这个问题。