你沒有傳遞正確的參數@ Html.DropDownList。從MSDN文檔在這裏:http://msdn.microsoft.com/en-us/library/dd470380(v=vs.100).aspx
它看起來像你想使用以下過載:
public static MvcHtmlString DropDownList(
this HtmlHelper htmlHelper,
string name,
IEnumerable<SelectListItem> selectList,
Object htmlAttributes
)
所以,你的第一個參數是你有正確的名稱字符串,但隨後你需要選擇列表的作爲你的第二個參數和你的HtmlAttributes作爲你的第三個參數。試着這樣說:
<div class="editor-field">
@Html.DropDownList("StandID", ViewBag.StandID, new {style = "width:150px"})
</div>
UPDATE:
不知道你正在傳遞正確的事情到您的ViewBag無論是。您將它設置爲等於一個新的SelectList對象,並且DropDownList需要一個SelectListItems集合。
試試這個在您的控制器:
var stands = db.Stands.ToList().Where(s => s.ExhibitorID == null)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.Description + "-- £" + s.Rate.ToString()
});
ViewBag.StandID = stands;
UPDATE:
這是我如何做到同樣的事情。我有一個靜態方法返回一個IEnumerable然後我在我的視圖中引用該方法。 (對不起VB語法)
Namespace Extensions
Public Module Utilities
Public Function SalutationSelectList(Optional ByVal Salutation As String = "") As IEnumerable(Of SelectListItem)
Dim ddl As New List(Of SelectListItem)
ddl.Add(New SelectListItem With {.Text = "", .Value = "", .Selected = If(Salutation = "", True, False)})
ddl.Add(New SelectListItem With {.Text = "Mr.", .Value = "Mr.", .Selected = If(Salutation = "Mr.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Ms.", .Value = "Ms.", .Selected = If(Salutation = "Ms.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Mrs.", .Value = "Mrs.", .Selected = If(Salutation = "Mrs.", True, False)})
ddl.Add(New SelectListItem With {.Text = "Dr.", .Value = "Dr.", .Selected = If(Salutation = "Dr.", True, False)})
Return ddl
End Function
End Module
End Namespace
@Html.DropDownListFor(Function(org) org.Salutation, SalutationSelectList())
DropDownList是否有一個有效的接口,接受第二個Int參數? –
@BenFinkel - 你在看舊的版本的帖子,我已經改變了不正確的參數。 –
不知道我在做什麼 - 但關閉,重新加載 - 我不再收到錯誤 - 非常感謝。 – Mark