19 November 2026

ASP.NET Core 11 Blazor DisplayName for Beginners: A Student Form

  Asp.Net CS By Example       19 November 2026

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.

One model name, one visible label

[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

  1. [Display(Name = "Student name")] defines the text that DisplayName shows. If the model property is renamed, the label stays meaningful.
  2. For="@(() => student.Name)" points the label and validation message at the same field.
  3. [Required] and DataAnnotationsValidator do the validation. DisplayName only displays a name; it does not validate or save data.
  4. @rendermode InteractiveServer lets 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

  1. Add an Email property with [Required] and [EmailAddress], then create a matching label, input and validation message.
  2. Change the display name to Full student name and check the visible label.
  3. 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.

logoblog

Thanks for reading ASP.NET Core 11 Blazor DisplayName for Beginners: A Student Form

Newest
You are reading the newest post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.