Asp.net mvc3 use of ListBox and dropdownlist in @ html

Source: Internet
Author: User

ListBox and dropdownlist return the select element by using the names of the specified HTML helper and form fields.

Same as selectextensions class

1. @ html. ListBox ()
Returns multiple select elements by using the specified HTML helper, form field name, specified list items, and specified HTML features.
The main parameters are:
String name, // name of the form field to be returned
Ienumerable <selectlistitem> selectlist, // a set of selectlistitem objects used to fill the drop-down list.
Idictionary <string, Object> htmlattributes // an object that contains the HTML features to be set for this element. You can set the width and other styles.

@ {List <string> ListBox = new list <string> (); ListBox. add ("first"); ListBox. add ("second"); ListBox. add ("third") ;}{ list <string> listboxselectd = new list <string> (); listboxselectd. add ("first"); listboxselectd. add ("second ");}


For example, bind the ListBox information and set "second" as the selected status to specify a selected status.
Call:

@ HTML. listBox ("listname", new selectlist (ListBox, "second"), new {style = "width: 200px"}) output: <select id = "listname" multiple = "multiple" name = "listname" style = "width: 200px "> <option> first </option> <option selected =" selected "> second </option> <option> third </option> </SELECT>

You can use new multiselectlist () to return multiple selected values.

@ HTML. listBox ("morelistname", new multiselectlist (ListBox, listboxselectd) output: <select id = "morelistname" multiple = "multiple" name = "morelistname"> <option selected = "selected"> first </option> <option selected = "selected"> second </option> <option> third </option> </SELECT>

Obtain selected items:
When submitting the page, use the request or formcollection collection object to obtain the value.
For example, when request ["listname"] and collection ["morelistname"] obtain multiple selected values, return results separated ","
B .@Html.ListBoxFor ()
Returns multiple select elements by using the specified HTML helper, form field name, specified list items, and specified HTML features.
Common Methods:

@Html.ListBoxFor(mo => mo.name, new SelectList(Model.Students, "ID", "Name"),new {style="width:200px"})

Model. Students: the output list object.
ID and name are two attributes of the object.
2. @ html. dropdownlist ()
Returns the select drop-down list box.
Example:

@ HTML. dropdownlist ("ddlvalue", new selectlist (new [] {"one", "two"}, "two"), new {style = "width: 200px"}) output: <select id = "ddlvalue" name = "ddlvalue" style = "width: 200px "> <option> one </option> <option selected =" selected "> two </option> </SELECT>

@ Html. dropdownlistfor ()
Similar to @ html. listboxfor (),

@Html.DropDownListFor(mo => mo.name, new SelectList(Model.Students, "ID", "Name"),new {style="width:200px"})

Return to list box

Related Article

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.