The Select Tag Helper generates HTML select controls and option elements from Razor model properties and item collections. Add asp-for to identify the bound property and asp-items to provide the available options. section 18.2.4
Use the bound property and item collection to choose the select behavior:
- Bind to a standard property for a drop-down list. section 18.2.4
- Bind to an IEnumerable property to create a multiselect list box. section 18.6
- Supply an IEnumerable<SelectListItem>; convert application-specific collections before passing them to the helper. section 18.2.4
- Assign SelectListGroup objects to item Group properties to generate optgroup elements. section 18.6
Three select styles
public IEnumerable<SelectListItem> Items { get; set; } = new List<SelectListItem>\n{\n new SelectListItem { Value = "csharp", Text = "C#" },\n new SelectListItem { Value = "python", Text = "Python" }\n};\n\n<select asp-for="Input.SelectedValue1" asp-items="Model.Items"></select>\n<select asp-for="Input.SelectedValue2" asp-items="Model.Items" size="4"></select>\n<select asp-for="Input.MultiValues" asp-items="Model.Items"></select>\n\nThe first select is a drop-down, the second is a single-select list box, and the third is multiselect when MultiValues is an IEnumerable property.
For enum options, use Html.GetEnumSelectList<TEnum>() instead of writing the SelectListItem mapping yourself. Add an option with an empty value when the form needs an explicit “not selected” choice; otherwise the select always has a value. section 18.2.4
