1

我的帐户控制器出现以下错误:

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);

        }

我不知道为什么会这样?

4

1 回答 1

1

看起来问题在这里:

Roles.AddUserToRole(model.UserName, "roleName");

我猜你并不是要把“roleName”写成文字。可能是 model.RoleName或“User”,还是您的角色提供者中存在的角色?

于 2012-04-15T01:07:54.283 回答