Ajax Redirect to Page instead of Updating Target

旧巷老猫 提交于 2019-12-01 02:35:21

问题


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

回答1:


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>


来源:https://stackoverflow.com/questions/1963808/ajax-redirect-to-page-instead-of-updating-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!