I'm trying to populate a dropdown in my view. Any help is greatly appreciated. Thanks.
Error:
Unable to cast the type 'System.Int32' to type 'System.Object'.
LINQ to Entities only supports casting Entity Data Model primitive types.
Controller:
ViewBag.category = (from c in new IntraEntities().CategoryItems
select new SelectListItem() {Text=c.Name, Value=""+c.ID }).ToList<SelectListItem>();
View:
Category:<br />@Html.DropDownList("category", (List<SelectListItem>)ViewBag.category)
Best Answer
How about this:
and how about using strongly typed view models instead of some weakly typed crap of a ViewBag (it's the way I call it)?
Like this:
then:
and finally in your strongly typed view:
Much better, don't you think?