2

用户注册后如何根据角色将用户重定向到不同的页面

我试图在没有任何运气的情况下将以下内容实施到帐户控制器中。

// POST: /Account/Register
         [AllowAnonymous]
         [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)
                 {
                     FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                     Roles.AddUserToRole(model.UserName, model.Role);

                     {
                         if (Roles.IsUserInRole("Student"))
                         {

                             return RedirectToAction("create, "Student");
                         }
                         else if (Roles.IsUserInRole("Teacher"))
                         {

                             return RedirectToAction("create, "Teacher");
                         }

                         return RedirectToAction("index", "Home");
                     }



                     List<SelectListItem> list = new List<SelectListItem>();
                     SelectListItem item;
                     foreach (String role in Roles.GetAllRoles())
                     {
                         item = new SelectListItem { Text = role, Value = role };
                         list.Add(item);
                     }

                     ViewBag.roleList = (IEnumerable<SelectListItem>)list;







                     ModelState.AddModelError("", ErrorCodeToString(createStatus));




                     // If we got this far, something failed, redisplay form
                     return View(model);
                 }
             }
         }

欢迎任何帮助或建议

4

1 回答 1

2

创建了一个新视图并添加了以下代码,

@{
        ViewBag.Title = "CreateProfilePage";
    }

    <h2>CreateProfilePage</h2>



    @if (User.IsInRole("Student"))
     {  




      <div> @Html.ActionLink("Create Landlord Profile", "Create", "Student") </div> 




     } 


      @if (User.IsInRole("Teacher"))
     {  


      <div> @Html.ActionLink("Create Teacher Profile", "Create", "Teacher") </div> 

帐户控制器中注册的重定向也更改为新视图。

// POST: /Account/Register
         [AllowAnonymous]
        [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)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    Roles.AddUserToRole(model.UserName, model.Role);
                   /////changed here/// return RedirectToAction("CreateProfilePage", "Property");
                }
                else
                {


                    List<SelectListItem> list = new List<SelectListItem>();
                    SelectListItem item;
                    foreach (String role in Roles.GetAllRoles())
                    {
                        item = new SelectListItem { Text = role, Value = role };
                        list.Add(item);
                    }

                    ViewBag.roleList = (IEnumerable<SelectListItem>)list;







                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

该代码仅显示一个链接,具体取决于登录用户的角色。

于 2012-04-25T22:35:09.437 回答