Dropdownlist is different from textbox and other controls. It uses the select flag. It requires two values: the list displayed in the drop-down box and the default options. Automatic Binding can only bind one attribute at a time. Therefore, you need to select the binding list or the default option as needed.
Each overload version of The dropdownlist extension method is "basically" passed to this method:
Code
Public static string dropdownlist (
This htmlhelper,
String name,
Ienumerable <selectlistitem> selectlist,
String optionlabel,
Idictionary <string, Object> htmlattributes ){
}
If selectlist is not specified, this method will automatically bind the list, that is, find the value corresponding to name from viewdata. If selectlist is provided, the default option is automatically bound, that is, the selectedlistitem with the selected Attribute true is found from selectlist. (For details, see the selectinternal auxiliary method of the htmlhelper method)
Example 1: If the action method contains the following code:
Use this in view:
Code
List <selectlistitem> items = new list <selectlistitem> ();
Items. Add (New selectlistitem {text = "Kirin", value = "29 "});
Items. Add (New selectlistitem {text = "Jade", value = "28", selected = true });
Items. Add (New selectlistitem {text = "Yao", value = "24 "});
This. viewdata ["list"] = items;
<% = Html. dropdownlist ("list") %> the auxiliary method first obtains the item whose key is list from viewdata. If the item is ienumerable <selectedlistitem>, it is bound to the drop-down box, otherwise, invalidoperationexception is thrown. Because the selected of the second selectlistitem is true, the second item is selected by default.
Example 2: If the code in the action is as follows:
Code
List <selectlistitem> items = new list <selectlistitem> ();
Items. Add (New selectlistitem {text = "Kirin", value = "29 "});
Items. Add (New selectlistitem {text = "Jade", value = "28 "});
Items. Add (New selectlistitem {text = "Yao", value = "24 "});
This. viewdata ["list"] = items;
This. viewdata ["selected"] = 24;
The code in view is as follows:
<% = Html. dropdownlist ("selected", viewdata ["list"] As ienumerable <selectlistitem>) %>
Then, the auxiliary method binds viewdata ["list"] to a drop-down box and obtains the items whose key is selected from viewdata, set the selectelistitem with the same value as the value in the list to the default one.
Although the above two methods can achieve the correct display of dropdownlist, it is not the best practice. In actual projects, we prefer to use a strong type in the code. For example, in the above two examples, the text and value of selectlistitem are the name and age attributes of the user object, but the above Code does not reflect this ing. If the user list is obtained from the database or other external resources, do we need to bind it in this way?
VaR users = getusers ();
Foreach (VAR user in users)
{
Items. Add (New selectlistitem {text = user. Name, value = user. Age. tostring ()});
}
This is obviously intolerable. So what is best practice?
ASP. net mvc provides a secondary type: selectlist for dropdownlist and ListBox (both use the select mark in HTML. Selectlist is inherited from multiselectlist, which implements ienumerable <selectlistitem>. That is to say, selectlist can be directly used as the second parameter of the HTML. dropdownlist method.
Multiselectlist contains four attributes:
Items: list used in the select tag, which is usually indicated by the option tag. Ienumerable type.
Datatextfield: Specifies the text item of option, which belongs to the string type.
Datavaluefield: Specifies the value of option, which belongs to the string type.
Selectedvalues: value of the selected item, ienumerable type.
Obviously, as dropdownlist, the selected item cannot be ienumerable, so selectlist provides a new attribute:
Selectedvalue: value of the selected item, object type.
Meanwhile, the selectlist constructor is as follows:
Code
Public selectlist (
Ienumerable items,
String datavaluefield,
String datatextfield,
Object selectedvalue): Base (items, datavaluefield, datatextfield, toenumerable (selectedvalue ))
{
Selectedvalue = selectedvalue;
}
So our code becomes:
VaR users = getusers (); var selectlist = new selectlist (users, "Age", "name", "24"); this. viewdata ["list"] = selectlist; direct reference in view: <% = html. dropdownlist ("list") %>
Of course, you can also use the constructor overload without the selectedvalue parameter, while explicitly specifying ienumerable <selectlistitem> In the view, specify other items with the same name as dropdownlist in viewdata or view model as the default option.
Finally, let's review three usage methods of dropdownlist:
Create an ienumerable <selectlistitem> and specify the default option.
Create an ienumerable <selectlistitem> and specify the default selected items in the properties of the viewdata item or view model.
Use selectlist.
Okay. We will discuss the usage of dropdownlist today. will you use it?