开发者

bind drop down?

开发者 https://www.devze.com 2022-12-11 09:23 出处:网络
in the aspx page i am getting this error while binding dropdown list Unable to cast object of type \'System.Web.Mvc.SelectList\' to type

in the aspx page i am getting this error while binding dropdown list

Unable to cast object of type 'System.Web.Mvc.SelectList' to type 'System.Collections.Generi开发者_开发问答c.IList`1[System.Web.Mvc.SelectListItem]'.

I have written:

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

please tell me the correct way of writing.

thanks

ritz


Here is a nice example of exactly what you are trying to accomplish:

How to bind IList with MVC Dropdownlist box

It looks like you will have to add some code-behind code to build the compatible list type.


what is the code in the controller action that you use to generate viewdata["categoryId"], here is what i normally do in the action code:

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

and then in your view, you just need:

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

精彩评论

暂无评论...
验证码 换一张
取 消