liveBook · Manning

Input tag helper

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

The Input Tag Helper is an ASP.NET Core Tag Helper that generates and formats HTML input elements from a bound property and its metadata.

The Input Tag Helper turns a bound property into a configured HTML input. Add asp-for to an input, and ASP.NET Core uses the property's type and DataAnnotations to choose the HTML type, value, name, id, and validation metadata. section 18.2.3

Binding checkout details

With a page model exposing [BindProperty] public UserBindingModel Input { get; set; }, a Razor form can use <input class="form-control" asp-for="Input.Email" />. Because Email has [EmailAddress], the helper produces an email input whose id is Input_Email, whose name is Input.Email, and whose value comes from the property.

Common mappings include:

  • Validation annotations add data-val-* attributes for client-side validation; the required jQuery validation scripts must also be included. section 18.2.3

The generated markup preserves model-binding names such as Input.Email, aligns the input id with a label's for attribute, carries the property's current value, and exposes validation messages through data-val-* attributes. You can still add ordinary HTML attributes when the generated output needs customization. section 18.2 section 18.2.3

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

How deep it goes
Explains type and annotation mappings, generated attributes, formatting, textareas, and validation scripts without descending into helper internals.
Examples
Uses checkout user fields, an email input, generated HTML, decimal formatting, and a multiline textarea.
What it assumes
Assumes a Razor view and a bound property, while demonstrating the model and annotations needed to drive generation.
What you can do after
Apply it via Razor with asp-for to generate appropriately typed inputs and validation metadata from bound properties.
How it connects
Connects Razor Pages, model binding, DataAnnotations, HTML5 input types, labels, validation messages, and client-side validation.

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.