简体   繁体   English

ASP.NET MVC 3-ajax调用后,重定向到新页面或生成页面刷新

[英]ASP.NET MVC 3 - After ajax call, redirect to new page or generate page refresh

Scenario: 场景:

I have written an MVC wizard that automatically uses ajax if javascript is enabled in the browser. 我已经编写了一个MVC向导,如果在浏览器中启用了javascript,它会自动使用ajax。 Each step of the wizard is a PartialView (I'm using ASP.NET MVC 3). 向导的每个步骤都是PartialView(我正在使用ASP.NET MVC 3)。

All works fine. 一切正常。

The problem is refreshing the page if, for example, the user's status changes as a result of how she fills in the wizard. 例如,如果用户的状态由于其填写向导的方式而发生更改,则问题在于刷新页面。 EG, if the wizard logs the user in, or registers them. EG(如果向导已登录或注册用户)。 As the wizard 'moves' from step to step by getting Partial Views via AJAX, the page doesn't get refreshed to reflect the change in the user's status (eg, an anonymous user is now registered). 随着向导通过AJAX逐步获取“部分视图”而逐步移动时,页面不会刷新以反映用户状态的更改(例如,现在注册了匿名用户)。

What I want to do: 我想做的事:

Essentially, when I need a full page refresh, I need to AUTOMATICALLY run the following on the client AFTER delivering the Partial View corresponding to the current step of the wizard via AJAX: 本质上,当我需要刷新整页时,需要通过AJAX在交付与向导当前步骤相对应的Partial View之后,在客户端上自动运行以下命令:

location.reload();

The Problem: 问题:

As the DOM has been modified via AJAX, I don't know how to make my javascript (location.reload();) run WITHOUT user intervention (eg, clicking a button). 由于DOM已通过AJAX进行了修改,因此我不知道如何在无需用户干预(例如单击按钮)的情况下运行javascript(location.reload();)。

Any suggestions? 有什么建议么? I have been off work for a while and am struggling to get back up to speed. 我已经下班了一段时间,正努力恢复速度。

For now, I have solved my problem using the code in the following article: 现在,我已经使用以下文章中的代码解决了我的问题:

Redirecting after AJAX call, when using ASP.NET MVC 使用ASP.NET MVC时,在AJAX调用后进​​行重定向

I like the approach discussed in the article because it results in reusable code, in a very MVC way - I already have a base controller ( AjaxController.cs ) where I encapsulate all my AJAX aware code and this is a nice addition to it. 我喜欢本文中讨论的方法,因为它可以以非常MVC的方式生成可重用的代码-我已经有了一个基本控制器( AjaxController.cs ),其中封装了我所有可AjaxController.cs AJAX的代码,这是对它的很好补充。

However, there are two issues with this approach: 但是,此方法存在两个问题:

  1. I have to redirect away from my wizard, so it only works for the final step of the wizard. 我必须重定向离开向导,因此它仅适用于向导的最后一步。 If the user's status changes half way through the wizard, I am still jiggered. 如果用户的状态在向导中途更改,我仍然会感到困惑。

  2. I would still like to know how to refresh my page after an AJAX call, rather than just redirecting like this. 我仍然想知道如何在AJAX调用后刷新页面,而不仅仅是这样重定向。

So if anyone has the answer, I will gladly give them the biscuit. 因此,如果有人有答案,我将很乐意为他们提供饼干。

I am not quite sure how your ajax calls are structured, but if you are using the MVC Ajax helper you can just call location.reload(); 我不太确定您的ajax调用的结构如何,但是如果您使用的是MVC Ajax帮助器,则可以仅调用location.reload(); in the OnComplete method, like so: 在OnComplete方法中,如下所示:

@using (Ajax.BeginForm(new AjaxOptions{OnComplete = "javascriptfunction"}))
{
    //Data and submit button
}
<script>
    function javascriptfunction(){
        location.reload();
    }
</script>
// C# code
public ActionResult Foo(Bar bar)
{
    // code here
    return Json(new
    {
        Success = true,
        Value = this.RenderPartialViewToString("PartialViewName", bar),
        Callback = "window.location.href = 'http://yourdomainurl.com/';"
    });
}


// jQuery Ajax
$.ajax({
    type: "POST",
    url:  "/urlWhereToPost",
    data: ("form#id").serialize(), 
    dataType: 'json',
    async: false,
    beforeSend:  function() {
        // some instruction
    },
    error: function(e) {
        alert(e.responseText);
    },      
    success: function(data) {
        if (data.Success) {
            if (data.Callback != null) {
                if (data.Callback.length > 0) {
                    jQuery.globalEval(data.Callback);
                }
            }
        }
        else {
            // do something
        }
    }
 });        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM