IHtmlHelper.ListBox(String, IEnumerable<SelectListItem>, Object) Method

Definition

Returns a multi-selection <select> element for the expression. Adds <option> elements based on selectList. Adds a "selected" attribute to an <option> if its Value (if non-null) or Text matches an entry in the first non-null collection found in: the ModelState entry with full name, the ViewData entry with full name (unless used instead of selectList), or the expression evaluated against Model. See Name(String) for more information about a "full name".

C#
public Microsoft.AspNetCore.Html.IHtmlContent ListBox (string expression, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Mvc.Rendering.SelectListItem> selectList, object htmlAttributes);

Parameters

expression
String

Expression name, relative to the current model.

selectList
IEnumerable<SelectListItem>

A collection of SelectListItem objects used to populate the <select> element with <optgroup> and <option> elements. If null, finds the SelectListItem collection with name expression in ViewData.

htmlAttributes
Object

An Object that contains the HTML attributes for the <select> element. Alternatively, an IDictionary<TKey,TValue> instance containing the HTML attributes.

Returns

A new IHtmlContent containing the <select> element.

Remarks

Combines HtmlFieldPrefix and expression to set <select> element's "name" attribute. Sanitizes expression to set element's "id" attribute.

Applies to

Product Versions
ASP.NET Core 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0