3
Answers

MVC SelectList error

Rich

Rich

12y
2.8k
1
I have a SelectList that I have passed into a form in my view.  When I try to post it I get this error:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'roleName'.

Code in Controller:
ViewBag.roleName = new SelectList(Roles.GetAllRoles().Where(rn =>
                rn != "Administrator" && rn != "Special"));
Code in View:
@Html.DropDownList("roleName", ViewBag.roleName as SelectList, "Select")

Answers (3)