Let’s see how to use the label tag helper in a form. We have the following model which will be used by our example.
using System.ComponentModel.DataAnnotations;
public class Student
{
[Display(Name="Student's first name")]
public string FirstName { get; set; }
}
Now at the top of the view we define the model which will be used by our view.
@model Student
Finally, we have the form tag helper which contains our label tag helper.
<form asp-controller="Home" asp-action="Index">
<label asp-for="FirstName"></label>
<input asp-for="FirstName" />
<input class="btn btn-success" type="submit" />
</form>
This code will generate the following HTML.
<form action="/" method="post">
<label for="FirstName">Student's first name</label>
<input type="text" id="FirstName" name="FirstName" value="" />
<input class="btn btn-success" type="submit" />
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8Cc6bJ3TWmVAoCQJZSt8GLvp87fi7pdgbRge0_5dUUxbwCioXGz-SmkDw7vFNeVtZ2wMFAyHo6vuC68LMaBfIOZX53WNpPO05D3CLDPwI959yviDV3ZA4goj8H1FiYlL2o4XEGoVPzK_zzZ_IhJdd7Q" />
</form>