i am creating a MVC3 application where the user can be either an Admin or a Data Entry, so i need to make use of the Role in user authentication that MVC offers. So in my Register Form I Have added
<%=
Html.DropDownList("RoleName", new SelectListItem[]{
new SelectListItem{ Text= "Admin", Value="admin"},
new SelectListItem{ Text= "Data Entry", Value="data"}
}) %>
And i modified the register controller as follows:
if (createStatus == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(model.UserName, "RoleName");
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
Now when i register i get the following error:
The role ” was not found.
Thank you …
Take a look at the definition for
Roles.AddUserToRole()method here. You are hardcoding “RoleName” for the second parameter which is supposed the role to add the user to.What you want to do is retrieve the selected value from your
DropDownListand use that string in yourRoles.AddUserToRole()method call.