The asp-for attribute connects a Razor form element to a PageModel property. Tag Helpers use that property’s type and metadata to generate suitable HTML, field names, values, and validation attributes. section 18.2.3 section 18.2.4
Generate an email input
Given an Email property decorated with [EmailAddress], write: <input asp-for="Input.Email" />. The Input Tag Helper generates an email input with id="Input_Email", name="Input.Email", the property’s current value, and data-val-* attributes for client-side validation. The dotted name preserves model binding when the form is posted.
The generated element depends on the bound property and its metadata:
- DataAnnotations such as EmailAddress, Phone, Url, Password, and HiddenInput select specialized input types and add validation metadata where applicable. section 18.2.3
- On select elements, asp-for identifies the selected PageModel property while asp-items supplies the SelectListItem options; omit Model. from asp-for. section 18.2.4
