You can submit a Blazor form programmatically by using EditContent validation. In the following example, a keypress event function triggers when you press the Enter key. It validates the form editContent.Validate() and submits the form programmatically.
<EditForm Model="modelClass" Context=MyCurrentEditContext>
<input @onkeypress="@(async e => await KeyPress(MyCurrentEditContext, e))" @bind-value="modelClass.TextValue" />
<button type="submit">Store it</button>
</EditForm>
<p>@formValue</p>
@code {
private string formValue { get; set; }
ModelClass modelClass = new ModelClass();
private async Task KeyPress(EditContext editContext, KeyboardEventArgs key)
{
if (key.Code == @"Enter")
{
if (editContext.Validate())
{
formValue = "Form Submitted";
}
}
}
public class ModelClass
{
public string TextValue = "BlazorApp";
}
}
Share with