Defining forms using the EditForm component
Microsoft provides ready-made components to build forms. We will use them to provide create, edit, and delete functionality for customers.
Microsoft provides the EditForm
component and several form elements, such as InputText
, to make it easier to use forms with Blazor.
EditForm
can have a model set to bind it to an object with properties and event handlers for custom validation, as well as to recognize standard Microsoft validation attributes on the model class, as shown in the following code:
<EditForm Model="@customer" OnSubmit="ExtraValidation">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText id="name" @bind-Value="customer.CompanyName" />
<button type="submit">Submit</button>
</EditForm>
@code {
private Customer customer = new();
private void ExtraValidation()
{
// Perform any extra validation you want...