liveBook · Manning

Dropdown

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

A select element is an HTML form control that presents one or more options for user selection.

A select element lets a form present options for selection. In Razor, the Select Tag Helper can render a drop-down, list box, multiselect control, or grouped options. section 18.2.4

A Razor PageModel supplies two kinds of properties:

  • An option collection containing SelectListItem objects with Value and Text properties. section 18.2.4
  • A selected-value property for one choice, or an IEnumerable property for multiple choices. section 18.2.4
  • The asp-for attribute binds the selection, while asp-items supplies the displayed options. section 18.2.4

Three common forms

Use <select asp-for="Input.SelectedValue1" asp-items="Model.Items"></select> for a drop-down. Add size="4" for a single-select list box, or bind asp-for to Input.MultiValues for multiselect behavior. Add SelectListGroup values to create optgroup elements. Include an option with an empty value, such as **Not selected**, when no choice should be selected by default.

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

How deep it goes
Explains PageModel properties, SelectListItem conversion, Tag Helper attributes, grouping, and default-selection behavior.
Examples
Demonstrates currency selection, programming-language options, list-box sizing, multiselect binding, grouped options, and an empty choice.
What it assumes
Assumes Razor PageModel properties while teaching SelectListItem options and selected-value properties.
What you can do after
Apply it via ASP.NET Core Razor Tag Helpers to render and bind single, multiple, sized, or grouped selections.
How it connects
Connects select elements with Razor Pages, Tag Helpers, model binding, SelectListItem, and optgroup elements.

Also explained in

Compare how 10 books explain dropdown →

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.