liveBook · Manning

SelectListItem

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

SelectListItem is an ASP.NET Core MVC data structure representing one option in a select list, including its value, text, and selection state.

SelectListItem represents one option in an ASP.NET Core select list. Its Value supplies the submitted value, Text supplies the displayed label, and the collection order controls the option order. The Select Tag Helper expects an IEnumerable<SelectListItem> for asp-items. section 18.2.4

Use SelectListItem objects to prepare select-list data separately from the property that receives submitted values.

  • Store the available options in an IEnumerable<SelectListItem> property, commonly populated from application data or hardcoded values. section 18.2.4
  • Bind a select element with asp-for for the selected value and asp-items for the SelectListItem collection. section 18.2.4
  • Associate items with SelectListGroup objects through Group to produce grouped optgroup elements. section 18.2.4
  • Convert application-specific collections such as List<string> or List<MyClass> before passing them to the Select Tag Helper. section 18.2.4

A language option list

public IEnumerable<SelectListItem> Items { get; set; } = new List<SelectListItem> { new SelectListItem { Value = "csharp", Text = "C#" }, new SelectListItem { Value = "python", Text = "Python" }, new SelectListItem { Value = "js", Text = "JavaScript" } }; <select asp-for="Input.SelectedValue" asp-items="Model.Items"></select> For grouped choices, assign each item a SelectListGroup through its Group property. Add a separate option with an empty value when the form needs an explicit “not selected” choice. section 18.2.4

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

How deep it goes
Explains properties, collection requirements, grouping, enum generation, ordering, and explicit no-selection options without descending into framework internals.
Examples
Uses programming-language options, grouped optgroup choices, multiselect lists, and an explicit empty “not selected” option.
What it assumes
Assumes basic C# collections and Razor model binding, while teaching the required SelectListItem mapping from application-specific data.
What you can do after
Apply it via the Select Tag Helper to build dropdowns, list boxes, multiselect controls, and grouped option lists.
How it connects
Connects SelectListItem with the Select Tag Helper, model binding, SelectListGroup, enum options, and HTML select elements.

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.