注册后重定向取决于角色

时间:2012-04-25 21:04:23

标签: asp.net-mvc-3 entity-framework-4

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

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

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

欢迎任何帮助或建议

1 个答案:

答案 0 :(得分: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));
                }
            }

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