The DisplayName property, which is set for built-in form components, help maintain an alternate name. When the component’s input value is incorrect due to form validation, this alternate name is displayed in the error message.
The DisplayName property is supported by the following built-in components.
- InputCheckbox
- InputDate
- InputNumber
- InputRadioGroup
- InputSelect
- InputText
- InputTextArea
Please refer to the code example below.
[Index.razor]
@page "/"
@using System.ComponentModel.DataAnnotations;
<EditForm Model="@details">
<DataAnnotationsValidator />
<div>
<strong>Name:</strong>
<InputText @bind-Value="details.Name" />
<ValidationMessage For="@(() => details.Name)" />
</div>
<br />
<div>
<strong>Age:</strong>
<InputNumber @bind-Value="details.Age" DisplayName="Details: Age"></InputNumber>
<ValidationMessage For="@(() => details.Age)" />
</div>
<br />
<div>
<strong>Birth Date:</strong>
<InputDate @bind-Value="details.BirthDate" DisplayName="Details: Birthday" />
<ValidationMessage For="@(() => details.BirthDate)" />
</div>
<br />
<div>
<strong>Address:</strong>
<InputTextArea @bind-Value="details.Address" />
<ValidationMessage For="@(() => details.Address)" />
</div>
<br />
<div>
<strong>Classification:</strong>
<InputSelect @bind-Value="details.Classification">
<option value="">Select classification...</option>
<option value="Sedan">Sedan</option>
<option value="SUV">SUV</option>
<option value="MUV">MUV</option>
</InputSelect>
<ValidationMessage For="@(() => details.Classification)" />
</div>
<br />
<div>
<strong>Vehicle Name: @details.VehicleName</strong>
<InputRadioGroup @bind-Value="details.VehicleName">
@foreach (var option in VehicleNames)
{
<br /><InputRadio Value="option" /> @option
}
</InputRadioGroup>
<ValidationMessage For="@(() => details.VehicleName)" />
</div>
<br />
<div>
<strong>Loan Required:</strong>
<InputCheckbox @bind-Value="details.IsLoanRequired" />
<ValidationMessage For="@(() => details.IsLoanRequired)" />
</div>
<br />
<button type="submit">Submit</button>
</EditForm>
@code{
public Details details = new Details();
public List<string> VehicleNames = new List<string> { "Ciaz", "Vitara Brezza", "Ertiga" };
public class Details
{
[Required, Display(Name = "Details: Name")]
public string Name { get; set; }
[Required]
public int Age { get; set; }
[Required]
public DateTime BirthDate { get; set; }
[Required, Display(Name = "Details: Address")]
public string Address { get; set; }
[Required, Display(Name = "Details: Classification")]
public string Classification { get; set; }
[Required, Display(Name = "Details: Vehicle Name")]
public string VehicleName { get; set; }
[Required, Display(Name = "Details: Loan Required")]
public bool IsLoanRequired { get; set; }
}
}
Share with