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' 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")
Jignesh TrivediPosted May 15, 2012, 4:57 AM
Hi,
Text And Value fileds are you actual Proper name.
you can also try..
Example:
If you Role class
public class Role
{
public int RoleId{get;set;}
public string RoleName{get;set;}
}
then statement becomes....
var k= Roles.GetAllRoles().Where(rn => rn != "Administrator" && rn != "Special"); list = k.Select(r => new SelectListItem() { Text = r.RoleName, Value = r.RoleId }).ToList();
IList
return new SelectList(list, "Value", "Text");
Hope this help.
RichPosted May 15, 2012, 3:35 AM
Jignesh TrivediPosted May 15, 2012, 12:01 AM
Yor code look correct but I think you miss to pass value filed and text field.
example.
ViewBag.roleName = new SelectList(Roles.GetAllRoles().Where(rn =>
rn != "Administrator" && rn != "Special"), "ValueFiled", "TextFiled");
Here Valuefield and Textfield name of the property of roles class.
hope this help.