Live Chat Icon For mobile
Live Chat Icon

How do you submit a form programmatically in Blazor?

Platform: Blazor| Category: Forms and validation

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

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.