Reputation: 91
I am getting the following error on my account controller:
The role 'roleName' was not found.
But i have two roles admin, and gamers
the drop down list in my register view picks them up:
<label for="roleName">Select Role:</label>
@Html.DropDownList("roleName")
@Html.ValidationMessage("roleName")
In my account controller I have the following:
//
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(model.UserName, "roleName");
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
I dont know why this is happing?
Upvotes: 1
Views: 172
Reputation: 102773
Looks like the problem is here:
Roles.AddUserToRole(model.UserName, "roleName");
I'm guessing you didn't mean to write "roleName" as a literal. Maybe model.RoleName or "User", or a role that exists in your role provider?
Upvotes: 1