2011-01-26 49 views
0

我在使用@ Html.DropDownListFor錯誤

@Html.DropDownListFor(m=>Model.listItems) I am having the error message 

錯誤1無重載方法,它返回

> IEnumerable<SelectListItem> 

public IEnumerable<SelectListItem> ListItems 
    { 

'的函數DropDownListFor' 拍攝1個參數

但是,它工作正常@Html.DropDownList("listItems", Model.listItems)

爲什麼我用@ Html.DropDownListFor來顯示錯誤信息?

回答

0

沒有滿足這些參數的重載。換句話說,Html.DropDownListFor()只接受一個SelectListItem沒有方法。

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor.aspx

嘗試是這樣的:

public class YourViewModel 
{ 
    public SelectList YourSelectList { get; set; } 
    public string SelectedItem { get; set; } 
} 

然後在您的視圖:

@Html.DropDownListFor(c=>c.SelectedItem, Model.YourSelectList) 

當你的形式發佈的,則selectedItem將持有哪個項目在下拉列表中選擇。