The index action code in the HomeController controller is as follows:
PublicActionResult index ()
{
list<selectlistitem> list =NewList<selectlistitem> ()
{
NewSelectListItem () {text="Zhang San", value="001"},
NewSelectListItem () {text="John Doe", value="002"},
NewSelectListItem () {text="Harry", value="003"},
NewSelectListItem () {text="Zhao Liu", value="004"},
NewSelectListItem () {text="Tianqi", value="005", selected=true}
};
viewdata["NameList"] = list;
returnView ();
}
Corresponding view, bind data to view's html.dropdownlist and Html.listbox
<%
SelectList List=viewdata["NameList"] asSelectList;
%>
<!--Note Use the DropDownList or ListBox to specify the first parameter, name,
will automatically search for the corresponding value in the ViewData collection viewdatadirectory as the key.
The report exception is not found. So here the name parameter must be the same as the key value of the data stored in the ViewData -
<%=Html.dropdownlist ("NameList", list)%>
<!--You can also complete data binding by specifying the name directly, because it says it will go to viewdatadirectory to search -
<%=Html.listbox ("NameList")%>
<!--Because the same namelist is specified as the ID and name of the HTML control, we want to modify its ID because the ID is usually unique in HTML -
<%=Html.listbox ("NameList", List,New{ID="mynamelist"})%>
The resulting HTML code is as follows:
Original reproduced from: http://www.cnblogs.com/jhxk/articles/2630967.html