RedirectToAction之后没有视图显示

After RedirectToAction no View displays

本文关键字:视图 显示 之后 RedirectToAction      更新时间:2023-09-26

我有一个登录页面。验证后,我重定向到所需的页面。它重定向但是No

查看appers,而不是显示以前的登录页面视图。

这是我的Javascript代码

    function abc() {
        var email = document.forms["MyForm"]["Email"].value;
        var password = document.forms["MyForm"]["Password"].value;
        if (email.length == 0 || password.length == 0) {
            alert("Email and Password Field Required");
            return false;
        }
        else {

            $.ajax({
                 url: $("#MyForm").attr('action'),
                type: $("#MyForm").attr('method'),
                data: $("#MyForm").serialize(),
                success: function (data) {
                     alert("Invalid Email or Password");
                }
            });
        }
    }
</script>

,这是控制器(HttpPost)

    public ActionResult UserLogin(Models.UserModel selectedDocuments)
    {                                  
        if (ModelState.IsValid)
        {
            long AdminID = IsValid(selectedDocuments.Email, selectedDocuments.Password);
            if (AdminID != 0)
            {
                FormsAuthentication.SetAuthCookie(selectedDocuments.Email, false);
                if (RoleID == 1)
                {
                    Session["SystemAdmin"] = true;
                    Session["AdminID"] = AdminID;
                    return RedirectToAction("ClubInfo", "Admin");
                }
                if (RoleID == 2)
                {
                    Session["ClubAdmin"] = true;
                    Session["AdminID"] = AdminID;
                    return RedirectToAction("ClubInfo", "ClubAdmin");
                }
                if (RoleID == 3)
                {
                    Session["NewsAdmin"] = true;
                    Session["AdminID"] = AdminID;
                    return RedirectToAction("ClubInfo", "NewsAdmin");
                }
            }
            else
            {
                ModelState.AddModelError("", "Login Data Is Incorrect.");
            }
        }
        return Json(new { selectedDocuments = "Whatever you want to send" }, JsonRequestBehavior.AllowGet);
    }

谁来告诉我怎样才能成功。

由于表单是通过ajax调用发布的,视图将作为ajax调用的响应返回,用户将无法查看重定向的视图。

return Json代替reutrn RedirectToAction(),从客户端重定向:

return Json(new 
               { 
                RedirectUrl= Url.Action("ClubInfo", "Admin")
               }
               ,JsonRequestBehavior.AllowGet);

和ajax的in success函数:

success: function (data) {
                     window.location.href = data.RedirectUrl;
                }

我通过改变重定向来实现

返回Json (Url。操作("ClubInfo"、"Admin"));

和javascript中的

                $.ajax({
                 url: $("#MyForm").attr('action'),
                type: $("#MyForm").attr('method'),
                data: $("#MyForm").serialize(),
                success: function (result) {
                        window.location.href = result;

                },
                error:function ()
               {
                 alert("Invalid Email or password");
               }
            });