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