Label Tag Helper can be used to render label
for a model property. It replaces method Html.LabelFor
in previous versions of MVC.
Let's say you have a model:
public class FormViewModel
{
public string Name { get; set; }
}
In the view you can use label
HTML element and asp-for
tag helper:
<form>
<label asp-for="Name"></label>
<input asp-for="Name" type="text" />
</form>
This is equivalent to the following code in earlier versions of MVC:
<form>
@Html.LabelFor(x => x.Name)
@Html.TextBoxFor(x => x.Name)
</form>
Both code snippets above render the same HTML:
<form>
<label for="Name">Name</label>
<input name="Name" id="Name" type="text" value="">
</form>