我對ASP.NET MVC 2非常陌生,而且我正忙於使用實體模型中的數據填充下拉列表以填充<>。我懷疑我的LINQ可能是錯誤的,但它並沒有傷害找出爲什麼我的代碼不起作用。如何使用實體模型中的數據填充Html.DropDownListFor <>?
我設法預先填充另一個DropDownListFor <>用下面的代碼:
<div class="editor-field">
<% string[] Days = new string[] { "Monday", "Wednesday", "Friday" };%>
<%--<%: Html.DropDownList("Delivery Day", new SelectList(Days)) %>--%>
<%: Html.DropDownListFor(model => model.DeliveryDay, new SelectList(Days))%>
<%: Html.ValidationMessageFor(model => model.DeliveryDay)%>
</div>
我想現在填充列表如下:
<div class="editor-field">
<% List<string> categoryList = new List<string>();
var categories = from c in Model.Category select c.Category_Category;
foreach (object cata in categories)
{
categoryList.Add(cata.ToString);
}%>
<%: Html.DropDownListFor(model => model.Category_Category,new SelectList(categoryList))%>
<%: Html.ValidationMessageFor(model => model.Category)%>
</div>
這是發生了什麼:
找不到源類型爲'int?'的查詢模式的實現。 '選擇'未找到。
我該如何解決這個問題?
謝謝。我仍然在學習如何使用MVC。感謝您指出這一點 – Eon 2012-02-24 12:48:57
我會抓這個,並找到另一個點開始 – Eon 2012-02-24 12:50:19