So I'm a little confused I think on how some submission logic works in Blazor Server (and possibly might be the same case with Blazor WebAssembly).
I have an EditForm with a field and a submit button (of type Submit):
<EditForm EditContext="@_editContext" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<label for="firstName" class="form-label">First Name</label>
<InputText class="form-control" @bind-Value="_formModel.FirstName" />
<button type="submit" class="btn btn-primary">New Person</button>
</EditForm>
@code {
private void HandleValidSubmit()
{
System.Diagnostics.Debug.WriteLine("SUBMIT!");
}
private class FormModel
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Provide a first name")]
public string FirstName{ get; set; }
}
}
In the browser, I do the following:
- Provide input that will evaluate to being invalid based on my
FormModelandDataAnnotationsValidatorand I am told so with a single button click. This is expected. - I correct the value of the First Name text input, and with it still having focus in the browser, I click
Submit - All that happens is the DataValidation seems to have run (cool, that's always good), BUT clicking the
Submitbutton did not ever appear to trigger my C# code that runs OnValidSubmit. (As you can see in my example, I test withDebug.WriteLine
Is there a way to avoid this WHILE keeping the EditForm using OnSubmit/OnInvalidSubmit/OnValidSubmit and buttons with type="submit"?
(I read that there is a little bit of a difference when using buttons of type submit vs of type button in Blazor and State updates...no idea if that is a factor here)
From the user's point of view, they are pressing the button twice for a single action. Which of course can feel a little odd or confusing.
From my testing, this seems to be the case when DataAnnotationsValidator is involved, and this partially makes sense up until two button presses on the client are required to actually hit my C# code. (i.e. I understand that Data Validation is occurring when focus changes)
I feel like I'm partially misunderstanding something, and partially missing some extra code or setup in my form to avoid this.
Is there a way to do what I'm looking to do here, while keeping the DataAnnotationsValidator ?
Thanks in advance for any help!