A form label should explain what the user needs to enter. In ASP.NET Core 11, Blazor's new DisplayName component can read a friendly label from a model property. This student form shows how to keep the label in one place and use ordinary data annotation validation.
[Display(Name = "Student name")] → DisplayName component → Student name label
Set up a Blazor Web App
Install the .NET 11 SDK. Run dotnet new blazor -n StudentLabelsDemo -f net11.0 --interactivity Server, then open that folder. Create Components/Pages/StudentLabels.razor and paste in this complete component:
@page "/student-labels"
@rendermode InteractiveServer
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
<PageTitle>Student name form</PageTitle>
<h1>Student name form</h1>
<EditForm Model="@student" OnValidSubmit="Save" FormName="StudentNameForm">
<DataAnnotationsValidator />
<label for="student-name">
<DisplayName For="@(() => student.Name)" />
</label>
<InputText id="student-name" @bind-Value="student.Name" />
<ValidationMessage For="@(() => student.Name)" />
<button type="submit">Save</button>
</EditForm>
@if (message is not null)
{
<p>@message</p>
}
@code {
private StudentForm student = new();
private string? message;
private void Save() => message = $"Saved in this page: {student.Name}";
private sealed class StudentForm
{
[Required]
[Display(Name = "Student name")]
public string Name { get; set; } = string.Empty;
}
}
Run dotnet run and open the local address shown by the app, followed by /student-labels. Leave the field empty and select Save to see a required-field message. Enter a name and save again. The page shows a confirmation message; this example does not write to a database.
Read the important pieces
[Display(Name = "Student name")]defines the text thatDisplayNameshows. If the model property is renamed, the label stays meaningful.For="@(() => student.Name)"points the label and validation message at the same field.[Required]andDataAnnotationsValidatordo the validation.DisplayNameonly displays a name; it does not validate or save data.@rendermode InteractiveServerlets the example handle the form and update the message interactively.
When should you use DisplayName?
It helps when several forms or tables show the same model property. Microsoft recommends [Display] over [DisplayName] for the model attribute because [Display] supports more metadata, including localization. If there is no display attribute, the component falls back to the property name.
Practice
- Add an
Emailproperty with[Required]and[EmailAddress], then create a matching label, input and validation message. - Change the display name to
Full student nameand check the visible label. - Explain which line controls the label, which lines validate, and which line handles a valid save.
Continue with the ASP.NET Core student learning path. See Microsoft's ASP.NET Core 11 release notes and Blazor forms guide.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.