liveBook · Manning

Asp-items attribute

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

The asp-items attribute supplies the collection of SelectListItem objects from which a select control's option elements are generated.

The asp-items attribute supplies the IEnumerable<SelectListItem> used to generate a select element’s option elements. Pair it with asp-for to bind the selected value to a PageModel property. section 18.2.4

Populate a select list

Create UI items with Value and Text, then reference the collection in Razor: <select asp-for="Input.SelectedValue" asp-items="Model.Items"></select>. The order of Items controls the displayed option order. section 18.2.4

Use asp-items with these practical patterns:

  • Convert application-specific collections, such as List<string> or List<MyClass>, into SelectListItem objects before displaying them. section 18.2.4
  • Generate enum choices with Html.GetEnumSelectList<TEnum>(). section 18.2.4
  • Assign SelectListGroup instances to items to produce grouped optgroup elements. section 18.2.4

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

How deep it goes
Explains required item types, collection conversion, ordering, enum generation, grouping, and PageModel referencing.
Examples
Uses language-option collections, enum choices, grouped options, and a Razor select bound to model properties.
What it assumes
Assumes basic Razor and PageModel usage while requiring familiarity with IEnumerable<SelectListItem> for displayed options.
What you can do after
Apply it via the ASP.NET Core Select Tag Helper to render ordered, grouped, or enum-backed select options.
How it connects
Connects Select Tag Helper, asp-for, SelectListItem, PageModel, Razor, and enum options.

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.