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
