Label Tag Helper

The Label Tag Helper

The label tag helper generates appropriate for attribute values and content based on the PageModel property that is assigned to it. It has just one attribute:

Attribute Description
for An expression to be evaluated against the current page model

Notes link

The label tag helper is intended to work alongside the Input tag helper. It takes a property of the PageModel as a parameter to the asp-for attribute and renders the name of the property as a value for the label’s for attribute and as the content of the label tag. Assuming that the PageModel has a property named “Email:

  1. <label asp-for="Email"></label>

This renders as

  1. <label for="Email">Email</label>

Note that the closing tag is required. If you use a self-closing tag e.g. <label asp-for="Email" />, the content will not be rendered.

You can override the value that is rendered in the label in two ways. The first option is to provide an alternative value:

  1. <label asp-for="Email">Email Address</label>

Alternatively, you can use the Data Annotations Display attribute to change the content that is rendered:

  1. [Display(Name="Email Address")]
  2. public string EmailAddress { get; set; }

Both of these approaches will render as

  1. <label for="EmailAddress">Email Address</label>