liveBook · Manning

Asp-for attribute

As explained in ASP.NET Core in Action, 3e

The asp-for attribute is an ASP.NET Core Razor attribute that associates an HTML element with a model property for Tag Helper processing.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains generated identifiers, values, input types, formatting, validation attributes, and select behavior without descending into Tag Helper internals.
Examples
Uses email inputs, formatted decimals, textareas, drop-downs, single-select list boxes, and multiselect list boxes.
What it assumes
Assumes basic Razor markup and PageModel properties while demonstrating form binding and generated HTML through concrete examples.
What you can do after
Apply it via ASP.NET Core Razor Tag Helpers to bind form controls and predict their generated HTML and validation behavior.
How it connects
Connects model binding, Tag Helpers, DataAnnotations, client-side validation, textarea elements, and select lists.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.