开发者

MVC Drop Down List

This construction below using dropdownlist does not work!!

<% = Html.DropDownList (ddlUf, "All", New With (. Class = "text"})%> 

It only works if I do this:

<% = Html.DropDownList (ddlUf, "All")%>
开发者_如何学JAVA

Or this:

<% = Html.DropDownList (ddlUf, Nothing, New With (. Class = "text"})%> 

How do I solve this problem?

Thanks


use new{ @class = "text"})


I think you have the order of parameters mixed up. Assuming that ddlUf is a string with the DropDownList name, and "All" is supposed to be the default option, then you need something like this:

<% =Html.DropDownList(ddlUf, Model.FullListOfSelectListItems, "All", New With (. Class = "text") %>


There's some parameter order/validity issues here. The Html.DropDownList() method works by taking the following:

string name - assigns a name/id to the control

SelectList selectList - the list of options to appear in the drop down. You can construct this by creating a List and then using Linq to select the items from the list you want to appear in the drop down i.e:

List<string> myList = new List<string>();

//Add items to appear in the drop down.
myList.Add("Item1");
myList.Add("AnotherItem");
...

//Select all the items from the list and place them into a SelectList object.
SelectList dropDownList = new SelectList(from items in myList select items);

string optionLabel - the text for a default, empty item.

object htmlAttributes - a selection of HTML attributes to apply to the control.

So in your example you might want to go for something like:

@Html.DropDownList("MyDropDown", Model.MyConstructedSelectList, "Default item", new { @class = "text" }) 

For more information on using drop down lists with models in MVC, see this blog post: http://277hz.co.uk/Blog/Show/10/drop-down-lists-in-mvc--asp-net-

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜