2009-10-29 323 views
0

下面這個使用dropdownlist的結構不起作用!MVC下拉列表

<% = Html.DropDownList (ddlUf, "All")%> 

或者這樣:

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

如果我這樣做它只能

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

我該如何解決這個問題?

感謝

回答

1

使用new{ @class = "text"})

+0

看起來像他使用VB.NET給我。用於vb的 – Kezzer 2009-10-29 14:16:30

+0

可以使用New與{[Class] =「text」} – adriaanp 2010-01-05 21:56:21

0

我認爲你有混合了參數的順序。假設ddlUf與DropDownList的名稱的字符串,並"All"應該是默認選項,那麼你需要的東西是這樣的:

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

這裏有一些參數順序/有效性問題。該Html.DropDownList()方法通過採取以下:

字符串名稱 - 名稱/ ID分配給控制

的SelectList的SelectList - 選項列表出現在下拉。您可以通過創建一個列表,然後使用LINQ to選擇要顯示在下拉即列表中的項目構建這樣的:

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); 

串optionLabel - 文本的默認情況下,空項目。

object htmlAttributes - 選擇要應用於控件的HTML屬性。

所以,在你的榜樣,你可能想要去的東西,如:

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

欲瞭解更多有關使用下拉配車型列出了MVC,看到這篇博客文章: http://277hz.co.uk/Blog/Show/10/drop-down-lists-in-mvc--asp-net-