Usage of Html.listbox (example)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.