MVC DropDownList IEnumerable binding error

When you try to bind a model field with a dropdownlist in view, you need to create a SelectList in controller then pass that SelectList to view from ViewBag or ViewData, normally there will be a conversion happening from your collection to SelectList, if you are not careful, very likely you will get an error :

The ViewData item that has the key ‘xxx’ is of type ‘System.String’ but must be of type ‘IEnumerable <SelectListItem>’.

At line

@Html.DropDownListFor(m=>m.xxx, 
                ViewBag.List as SelectList,
            "Select an Item", new { id = "dp" })

I have searched a while for the cause of this error, some people said that it is because of validation error in POST, but it is not in my case. What I have found is you must create a SelectList using SelectList constructor for ViewBag data, in my case I created a list of IEnumerable<SelectListItem>.

 IEnumerable<SelectListItem> list= ….
   ViewBag.List = list
   return View();

The right way is you must create a SelectList

 IEnumerable SelectListItem list= ….
   ViewBag.List =  new SelectList(list, "Value","Text")
   return View();

The list can be any IEnumerable, does not have to be IEnumerable<SelectListItem> when you are using SelectList.

On the other hand you can also use IEnumerable<SelectListItem>, the key is you use same type in Html.DropDownListFor as well

 IEnumerable<SelectListItem> list= ….
   ViewBag.List = list
   return View();

And

@Html.DropDownListFor(m=>m.xxx, 
          ( IEnumerable<SelectListItem>)  ViewBag.List,
            "Select an Item", new { id = "dp" })

You do not mix the two.

The conclusion:

This error is basically a type casting error, type cast it to its orginal type either IEnumerable<SelectListItem> or SelectList in @Html.DropDownListFor.

Tags:

This entry was posted on Thursday, November 15th, 2012 at 8:01 pm and is filed under ASP.NET. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*