HtmlHelper.DropDownList Method (String, String, IEnumerable<SelectListItem>, Object, IDictionary<String, Object>)
Returns an HTML drop-down list control that has the specified name, custom attributes defined by an attribute dictionary, and default selection, and that contains the specified list items and default item.
Namespace: System.Web.WebPages.Html
Assembly: System.Web.WebPages (in System.Web.WebPages.dll)
Syntax
'Declaration
Public Function DropDownList ( _
name As String, _
defaultOption As String, _
selectList As IEnumerable(Of SelectListItem), _
selectedValue As Object, _
htmlAttributes As IDictionary(Of String, Object) _
) As IHtmlString
'Usage
Dim instance As HtmlHelper
Dim name As String
Dim defaultOption As String
Dim selectList As IEnumerable(Of SelectListItem)
Dim selectedValue As Object
Dim htmlAttributes As IDictionary(Of String, Object)
Dim returnValue As IHtmlString
returnValue = instance.DropDownList(name, _
defaultOption, selectList, selectedValue, _
htmlAttributes)
public IHtmlString DropDownList(
string name,
string defaultOption,
IEnumerable<SelectListItem> selectList,
Object selectedValue,
IDictionary<string, Object> htmlAttributes
)
public:
IHtmlString^ DropDownList(
String^ name,
String^ defaultOption,
IEnumerable<SelectListItem^>^ selectList,
Object^ selectedValue,
IDictionary<String^, Object^>^ htmlAttributes
)
member DropDownList :
name:string *
defaultOption:string *
selectList:IEnumerable<SelectListItem> *
selectedValue:Object *
htmlAttributes:IDictionary<string, Object> -> IHtmlString
public function DropDownList(
name : String,
defaultOption : String,
selectList : IEnumerable<SelectListItem>,
selectedValue : Object,
htmlAttributes : IDictionary<String, Object>
) : IHtmlString
Parameters
- name
Type: System.String
The value to assign to the name attribute of the HTML select element.
- defaultOption
Type: System.String
The text to display for the default option in the list.
- selectList
Type: System.Collections.Generic.IEnumerable<SelectListItem>
A list of SelectListItem instances that are used to populate the list.
- selectedValue
Type: System.Object
The value that specifies the item in the list that is selected by default. The selected item is the first item in the list whose value matches the parameter (or whose text matches, if there is no value.)
- htmlAttributes
Type: System.Collections.Generic.IDictionary<String, Object>
The names and values of custom attributes for the element.
Return Value
Type: System.Web.IHtmlString
The HTML markup that represents the drop-down list control.
Exceptions
Exception | Condition |
---|---|
ArgumentException | name is null reference (Nothing in Visual Basic) or empty. |
Remarks
The returned markup consists of an HTML select element in the following form:
<select name="name" ...htmlAttributes...>
<option>defaultOption</option>
<option value="selectList[0].Value">selectList[0].Text</option>
<option value="selectList[1].Value">selectList[1].Text</option>
<option value="selectList[selectedValue].Value" selected="selected">selectList[selectedValue].Text</option>
...
</select>
The default option appears at the start of the list and its value is empty. A default option is used to communicate that no option is being chosen from the list, such as when the drop-down control represents an optional parameter.