Test environment: vs2013,. Net4.5, MVC5
One, ASP. NET MVC Binding Control Principle Description
Taking Html.textbox as an example
/// <param name= "name" > name, corresponding with name and ID </param> /// <param name= "value" > Value , if value is null or does not exist, then value is automatically equal to name. The order is obtained by first looking for the existence of an entry from ViewData that has a key value of name, and if none is found in the ViewData, if there is a property named Name value from Viewdata.model, NULL is returned if it still does not exist. </param>// <returns></returns>public static mvchtmlstring TextBox (stringobject value);
Controller code:
viewdata["name" " Zhang San ";
View Code:
@Html. TextBox ("name")
Compile Code:
<id= " name" name= "name" type= "text" Value= "Zhang San"/>//The viewdata["name" value is automatically bound to
The same can be bound: label, RadioButton, CheckBox, TextArea, hidden, etc. input Label control
DropDownList and Texttbox are the same, but the DropDownList option needs to be a collection instead of a string value, so you need to use a method to bind
Two, ASP. NET MVC binding DropDownList 1, Method 1:List<selectlistitem> General binding Method
Controller code:
list<selectlistitem> items =NewList<selectlistitem>(); items. ADD (NewSelectListItem {Text ="Basketball", Value ="1"}); items. ADD (NewSelectListItem {Text ="Football", Value ="2", selected=true}); items. ADD (NewSelectListItem {Text ="Billiards", Value ="3" }); viewdata["Ddlqiu"] = items;
View Code:
@Html. DropDownList ("ddlqiu")
Compile Code:
<SelectID= "Ddlqiu"name= "Ddlqiu"><optionvalue= "1">Basketball</option><optionselected= "Selected"value= "2">Football</option><optionvalue= "3">Billiards</option></Select>
2. Method 2:selectlist Object Binding method
ASP. NET MVC prepares a secondary type for both the DropDownList and the ListBox (both use the SELECT tag in html): SelectList.
SelectList inherits from Multiselectlist, while the latter implements Ienumerable<selectlistitem>. In other words, selectlist can be used directly as the second parameter of the Html.dropdownlist method.
The multiselectlist contains four properties, respectively:
- Items: The list that appears in the select tag, usually denoted with the option tag. The IEnumerable type.
- DataTextField: The text item as option, String type.
- DataValueField: The value item as option, String type.
- Selectedvalues: The value of the selected item, the IEnumerable type.
Controller code:
var 0 ); var New " ID " " name " " 9 " ); viewdata["ddlcity"] = ddllist;
Voew Code:
@Html. DropDownList ("ddlcity")
Compile Code:
<SelectID= "Ddlcity"name= "Ddlcity"><optionvalue= "1">Beijing</option><optionvalue= "2">Tianjin</option><optionvalue= "3">Hebei Province</option><optionvalue= "4">Shanxi Province</option><optionvalue= "5">Inner mongolia autonomous region</option><optionvalue= "6">Liaoning Province</option><optionvalue= "7">Jilin Province</option><optionvalue= "8">Heilongjiang Province</option><optionselected= "Selected"value= "9">Shanghai</option><optionvalue= "Ten">Jiangsu Province</option><optionvalue= "One">Zhejiang Province</option><optionvalue= " a">Anhui Province</option><optionvalue= "+">Fujian</option><optionvalue= "+">Jiangxi Province</option><optionvalue= " the">Shandong Province</option><optionvalue= "+">Henan Province</option><optionvalue= "+">Hubei Province</option><optionvalue= "+">Hunan Province</option><optionvalue= "+">Guangdong Province</option><optionvalue= " the">Guangxi zhuang autonomous region</option><optionvalue= "+">Hainan</option><optionvalue= "All">Chongqing</option><optionvalue= "All">Sichuan Province</option><optionvalue= "+">Guizhou Province</option><optionvalue= "+">Yunnan Province</option><optionvalue= "+">Tibet autonomous region</option><optionvalue= "+">Shaanxi Province</option><optionvalue= "+">Gansu Province</option><optionvalue= "$">Qinghai Province</option><optionvalue= "+">Ningxia hui autonomous region</option><optionvalue= "+">Xinjiang uygur autonomous region</option><optionvalue= " the">Hong Kong S.A.R</option><optionvalue= " the">Macau S.A.R</option><optionvalue= "3525">Foreign</option></Select>
Both methods can be, like which to use which
ASP. NET MVC binding DropDownList and other controls