Ajax 重定向到页面而不是更新目标

发布于 2024-08-16 06:32:37 字数 1417 浏览 4 评论 0原文

我正在使用部分视图进行登录,并希望在成功时将用户重定向到新页面,并在模型无效时在部分视图中显示验证错误。 ajax目标正在更新,成功或失败。如果模型有效,它会在更新目标中显示整个新页面,但我希望它重定向到新页面。我尝试过 Redirect 和 RedirecttoAction 但没有得到预期的结果。关于如何获取 ajax 更新以重定向到新页面而不是更新目标的任何想法。另外,如果我使用了错误的方法,请告诉我。

部分视图代码:

<% using (Ajax.BeginForm(
        "LogOn", 
        null, 
        new AjaxOptions { 
            HttpMethod = "POST", 
            UpdateTargetId = "SignInForm" 
        }, 
        new { 
            id = "SignInForm",  ReturnUrl = Request.QueryString["ReturnUrl"] 
        })) { %>

                    <<Page HTML Controls>>

                    <input type="submit" value="Log On" />


            <% } %>

这是相关的控制器代码:

  public ActionResult Logon(LogOnModel model,string returnUrl)
        {
            if (ModelState.IsValid)
            {
            //Login Logic Code        
            if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "App");   
                    }

            }

            // If we got this far, something failed, redisplay form
            if (Request.IsAjaxRequest())
                           return PartialView("LogOnControl");

            return View(model);
        }

I am using a partial view for login and would like to redirect the user to a new page on success and show the validation errors in the partial view if the model is invalid. The ajax target is being updated and success or failure. If the the model is valid, it is showing the entire new page in the update target but I want it to redirect to the new page. I have tried Redirect and RedirecttoAction but it is not getting the desire results. Any ideas on what I can to go get an ajax update to redirect to a new page, not update the target. Also, let me know if I am using the wrong approach.

Partial View Code:

<% using (Ajax.BeginForm(
        "LogOn", 
        null, 
        new AjaxOptions { 
            HttpMethod = "POST", 
            UpdateTargetId = "SignInForm" 
        }, 
        new { 
            id = "SignInForm",  ReturnUrl = Request.QueryString["ReturnUrl"] 
        })) { %>

                    <<Page HTML Controls>>

                    <input type="submit" value="Log On" />


            <% } %>

Here is the relevant controller code:

  public ActionResult Logon(LogOnModel model,string returnUrl)
        {
            if (ModelState.IsValid)
            {
            //Login Logic Code        
            if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "App");   
                    }

            }

            // If we got this far, something failed, redisplay form
            if (Request.IsAjaxRequest())
                           return PartialView("LogOnControl");

            return View(model);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-08-23 06:32:37

要执行重定向,您需要在客户端执行。因此,您不能再使用 UpdateTargetId,而应该使用 OnSuccess 选项。您还需要修改 Logon 控制器操作,以便在重定向的情况下测试您是否是 ajax 请求,并在这种情况下返回一个带有重定向 url 的 Json 对象,该对象将在javascript:

if (ModelState.IsValid)
{
    if (string.IsNullOrEmpty(returnUrl))
    {
        returnUrl = Url.Action("Index", "App");
    }
    if (Request.IsAjaxRequest())
    {
        return Json(new { returnUrl = returnUrl });
    }
    return Redirect(returnUrl);
}

并在视图中:

<% using (Ajax.BeginForm(
    "LogOn", 
    null, 
    new AjaxOptions { 
        HttpMethod = "POST", 
        OnSuccess = "success" 
    }, 
    new { 
        id = "SignInForm", ReturnUrl = Request.QueryString["ReturnUrl"] 
    })) { %>
        <<Page HTML Controls>>
        <input type="submit" value="Log On" />
<% } %>

<script type="text/javascript">
function success(context) {
    var returnUrl = context.get_data().returnUrl;
    if (returnUrl) {
        window.location.href = returnUrl;
    } else {
        // TODO: update the target form element with the returned partial html
    }
}
</script>

To perform a redirect you need to do it on the client side. So you can no longer use UpdateTargetId but you should instead use the OnSuccess option. You will also need to modify the Logon controller action so that in case of a redirect you test if you it is an ajax request and in this case return a Json object with the redirect url which will be used in javascript:

if (ModelState.IsValid)
{
    if (string.IsNullOrEmpty(returnUrl))
    {
        returnUrl = Url.Action("Index", "App");
    }
    if (Request.IsAjaxRequest())
    {
        return Json(new { returnUrl = returnUrl });
    }
    return Redirect(returnUrl);
}

And in the view:

<% using (Ajax.BeginForm(
    "LogOn", 
    null, 
    new AjaxOptions { 
        HttpMethod = "POST", 
        OnSuccess = "success" 
    }, 
    new { 
        id = "SignInForm", ReturnUrl = Request.QueryString["ReturnUrl"] 
    })) { %>
        <<Page HTML Controls>>
        <input type="submit" value="Log On" />
<% } %>

<script type="text/javascript">
function success(context) {
    var returnUrl = context.get_data().returnUrl;
    if (returnUrl) {
        window.location.href = returnUrl;
    } else {
        // TODO: update the target form element with the returned partial html
    }
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文