The Label Tag Helper creates a label’s visible caption and its for attribute from a Razor Page property. Use asp-for instead of manually writing field names and element references. section 18.2.2
Binding labels to a model
Given a model with [Display(Name = "Your name")] on FirstName and no Display attribute on Email, write: <label asp-for="FirstName"></label> and <label asp-for="Email"></label>. The generated HTML is: <label for="FirstName">Your name</label> and <label for="Email">Email</label>. section 18.2.2
Use the helper with these behaviors:
- Reads [Display] to choose the caption; otherwise uses the property name. section 18.2.2
- Generates a for value that corresponds to the field element’s generated ID, supporting accessible label-field associations. section 18.2.2 section 18.2
- References nested properties with dotted expressions such as asp-for="Input.FirstName". section 18.2.2
- Preserves explicitly supplied label text instead of replacing it. section 18.2.2
