我的帐户控制器提供了以下代码:
// 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, "gamers");
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);
}
我有两个角色
gamers and consoleadders
我的问题是,我该如何设置 thsi 代码,以便当用户选择一个角色时,它会被保存,因为它目前只保存一个角色,即游戏玩家。我可以获得可见的角色,但我只能选择"gamers"
这是因为这行代码:
Roles.AddUserToRole(model.UserName, "gamers");
我还需要什么,所以当添加用户时,他们可以选择任一角色并将角色设置为该帐户
谢谢