jQuery AJAX 发布后视图不刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17742514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:52:25  来源:igfitidea点击:

View not refreshing after AJAX post

asp.net-mvcasp.net-mvc-3jqueryasp.net-mvc-4

提问by Danny van der Kraan

I have a view (Index.cshtml) with a grid (Infragistics JQuery grid) with an imagelink. If a user clicks on this link the following jquery function will be called:

我有一个带有图像链接的网格(Infragistics JQuery 网格)的视图(Index.cshtml)。如果用户单击此链接,将调用以下 jquery 函数:

function ConfirmSettingEnddateRemarkToYesterday(remarkID) {

    //Some code...

    //Call to action.
$.post("Home/SetEnddateRemarkToYesterday", { remarkID: remarkID },  function (result) {
    //alert('Succes: ' + remarkID);
    //window.location.reload();
    //$('#remarksgrid').html(result);
});

}

Commented out you can see an alert for myself and 2 attempts to refresh the view. The location.reload() works, but is basically too much work for the browser. The .html(result) posts the entire index.cshtml + Layout.cshtml double in the remarksgrid div. So that is not correct.

注释掉您可以看到我自己的警报和 2 次尝试刷新视图。location.reload() 有效,但对于浏览器来说基本上是太多的工作。.html(result) 在备注网格 div 中发布了整个 index.cshtml + Layout.cshtml double。所以这是不正确的。

This is the action it calls (SetEnddateRemarkToYesterday):

这是它调用的操作 (SetEnddateRemarkToYesterday):

       public ActionResult SetEnddateRemarkToYesterday(int remarkID) {

        //Some logic to persist the change to DB.

        return RedirectToAction("Index");
    }

This is the action it redirects to:

这是它重定向到的操作:

[HttpGet]
public ActionResult Index() {
    //Some code to retrieve updated remarks. 
    //Remarks is pseudo for List<Of Remark>
    return View(Remarks);

}

If I don't do window.location.reload after the succesfull AJAX post the view will never reload. I'm new to MVC, but i'm sure there's a better way to do this. I'm not understanding something fundamental here. Perhaps a nudge in the right direction? Thank you in advance.

如果我在成功的 AJAX 发布后不执行 window.location.reload,视图将永远不会重新加载。我是 MVC 的新手,但我确信有更好的方法来做到这一点。我不明白这里的一些基本知识。也许是朝着正确方向的推动?先感谢您。

回答by Satpal

As you requesting AJAX call, you should redirect using its response

当您请求 AJAX 调用时,您应该使用其响应重定向

Modify your controller to return JSONResult with landing url:

修改您的控制器以返回带有登陆 url 的 JSONResult:

public ActionResult SetEnddateRemarkToYesterday(int remarkID) {
    //Some logic to persist the change to DB.
    var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Controller");
        return Json(new { Url = redirectUrl });
}

JS Call:

JS调用:

$.post("Home/SetEnddateRemarkToYesterday", { remarkID: remarkID },  function (result) {
    window.location.href = result.Url
});

回答by Naveen Katakam

After Ajax post you need to call to specific Url.. like this.. window.location.href = Url

在 Ajax 发布后,您需要调用特定的 Url .. 像这样.. window.location.href = Url

回答by Colin

When using jQuery.postthe new page is returned via the .donemethod

jQuery

使用jQuery.post 时,新页面通过.done方法 返回

jQuery

jQuery.post("Controller/Action", { d1: "test", d2: "test" })
  .done(function (data) {
      jQuery('#reload').html(data);
  });

HTML

HTML

<body id="reload">