Hello! I have a RadzenTemplateForm where I collect data from the user. I implement a submit button and have the Submit method set on the form. If I edit an existing record everything works as intended. The page loads up the existing record - if the user doesn't make any changes then the EditContext.IsModified is false. If the user makes a change then the EditContext.IsModified is true.
But on a blank version of my form where the user creates a brand new record, the IsModified on the edit context is always true even after a successful save. I'm using this to prompt the user when they go to leave the page that they have unsaved data - so it always being true even after the initial save it causing problems for my users.
Here is my form:
<RadzenTemplateForm TItem="RateVariationRequest" Data=@rvr Submit="@OnRequestSubmit" @ref=@editForm>
<RadzenButton Text="Save" ButtonType="Radzen.ButtonType.Submit" />
@* my Radzen textbox, dropdown list, date picker, etc. *@
</RadzenTemplateForm>
My code that I use when they go to navigate away from the page that uses the editForm.EditContext:
async Task ConfirmInternalNavigation(LocationChangingContext locationChanging)
{
if (editForm != null && editForm.EditContext!.IsModified())
{
var result = await DialogService.Confirm("You have unsaved changes on this page. Are you sure you want to navigate away and lose your changes?",
"Unsaved Changes!", new ConfirmOptions() { OkButtonText = "Yes", CancelButtonText = "No" });
if (result == false)
{
locationChanging.PreventNavigation();
}
}
}
My save code:
async Task OnRequestSubmit()
{
//Save Request
request.ModifiedDate = DateTime.Now;
request.AddDate = DateTime.Now;
requestService.RequestInsert(Request);
successMessage = "Request Saved!";
pageType = "Review";
StateHasChanged();
}
}
The requestService is just a data layer that uses entity framework to insert records into our database. After the save of a new record and displaying a message to the user that the request is saved, the EditContext.IsModified is always true.
But if I go out of the page to our search screen, find the record I just created, select edit, and let it load it back into the page then the EditContext.IsModified functions as expected.
Is there a way to reset the IsModified after I do my insert?