views:

196

answers:

2
+1  Q: 

bind drop down?

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.Generic.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

+1  A: 

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.

jaywon
+1  A: 

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"])%>
tuanvt