阿贾克斯重定向到页面,而不是更新的目标 [英] Ajax Redirect to Page instead of Updating Target

查看:128
本文介绍了阿贾克斯重定向到页面,而不是更新的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用登录的局部视图,并想将用户重定向到成功一个新的页面,并显示验证错误的局部视图如果模型是无效的。 Ajax目标正在更新和成功或失败。如果该模型是有效的,它显示了更新的目标,整个新的页面,但我希望它重定向到新的一页。我曾尝试重定向和RedirecttoAction但没有得到期望的结果。在我所能去得到一个AJAX更新任何想法重定向到一个新的页面,而不是更新的目标。此外,让我知道如果我使用的是错误的做法。

管窥code:

 <%使用(Ajax.BeginForm(
        登录,
        空值,
        新AjaxOptions {
            列举HTTPMethod =POST
            UpdateTargetId =SignInForm
        },
        新{
            ID =SignInForm,RETURNURL =的Request.QueryString [RETURNURL]
        })){%GT;                    << HTML页控制与GT;>                    <输入类型=提交值=登录/>
            <%}%GT;

下面是相关的控制器code:

 公众的ActionResult登录(LogOnModel型号,串RETURNURL)
        {
            如果(ModelState.IsValid)
            {
            //登录逻辑code
            如果(!String.IsNullOrEmpty(RETURNURL))
                    {
                        返回重定向(RETURNURL);
                    }
                    其他
                    {
                        返回RedirectToAction(指数,应用程序);
                    }            }            //如果我们走到这一步,事情失败了,重新显示形式
            如果(Request.IsAjaxRequest())
                           返回PartialView(LogOnControl);            返回查看(模型);
        }


解决方案

要执行你需要做的是在客户端重定向。所以,你不能再使用 UpdateTargetId ,但你应该改用的onSuccess 选项。您还需要修改登录控制器动作,这样在重定向的情况下,你测试,如果你是一个Ajax请求,在这种情况下返回一个JSON对象的重定向网址将在JavaScript中使用:

 如果(ModelState.IsValid)
{
    如果(string.IsNullOrEmpty(RETURNURL))
    {
        RETURNURL = Url.Action(指数,应用程序);
    }
    如果(Request.IsAjaxRequest())
    {
        返回JSON(新{RETURNURL = RETURNURL});
    }
    返回重定向(RETURNURL);
}

和视图:

 <%使用(Ajax.BeginForm(
    登录,
    空值,
    新AjaxOptions {
        列举HTTPMethod =POST
        的onSuccess =成功
    },
    新{
        ID =SignInForm,RETURNURL =的Request.QueryString [RETURNURL]
    })){%GT;
        << HTML页控制与GT;>
        <输入类型=提交值=登录/>
<%}%GT;<脚本类型=文/ JavaScript的>
函数成功(上下文){
    VAR RETURNURL = context.get_data()RETURNURL。
    如果(RETURNURL){
        window.location.href = RETURNURL;
    }其他{
        // TODO:更新与返回的HTML部分目标表单元素
    }
}
< / SCRIPT>

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

解决方案

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>

这篇关于阿贾克斯重定向到页面,而不是更新的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆