我的帐户控制器出现以下错误:
The role 'roleName' was not found.
但我有两个角色管理员和游戏玩家
我的注册视图中的下拉列表会选择它们:
<label for="roleName">Select Role:</label>
@Html.DropDownList("roleName")
@Html.ValidationMessage("roleName")
在我的帐户控制器中,我有以下内容:
//
// 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);
}
我不知道为什么会这样?