0
我喜歡讓用戶在asp.net mvc web應用程序中註冊時選擇他們的角色。我有以前通過asp.net addministration工具添加角色。這裏是我使用的代碼註冊方法將用戶添加到註冊表失敗的角色ASP.NET MVC3
public ActionResult Register()
{
ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
return View();
}
[HttpPost]
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);
}
雖然註冊查看我得到這個代碼
<label for="roleName">Select Role:</label>
@Html.DropDownList("roleName")
@Html.ValidationMessage("roleName")
角色都寫在數據庫和用戶雖然。然而UsersInRoles表是空的,我得到這個異常
System.Configuration.Provider.ProviderException:找不到角色'roleName'。
有沒有人有同樣的問題?