2009-11-09 70 views
1

在aspx頁面我得到這個錯誤,而綁定下拉列表綁定下拉?

無法轉換的類型 「System.Web.Mvc.SelectList」對象鍵入 「System.Collections.Generic.IList`1 [System.Web.Mvc.SelectListItem]」。

我已經寫:

<p> 
    <label for="categoryId">Category:</label> 
    <%= Html.DropDownList("categoryId", (IList<SelectListItem>)ViewData["categoryId"])%> 
    <%= Html.ValidationMessage("categoryId", "*")%> 
</p> 

,請告訴我,寫的正確途徑。

感謝

麗思

回答

1

什麼是控制器動作時,用它來生成計算機[「的categoryId」]的代碼,這是我常做的動作代碼:

ArrayList categoryList=New ArrayList; 
     foreach (category c In YourcategoryCollection) 
{   categoryList.Add(New With {.Item = c.categoryName, .value = c.categoryID}) 
} 
    Viewdata("categoryId")=New SelectList(categoryList, "Value", "Item", itemToEdit.categoryID)} 

,然後在你看來,你只是需要:

<%= Html.DropDownList("categoryId", ViewData["categoryId"])%>