AJAX后视图后不刷新

AJAX后视图后不刷新

本文介绍了AJAX后视图后不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格(Infragistics的JQuery的网格)与一个ImageLink的视图(Index.cshtml)。如果用户点击该链接下面的jQuery函数将被要求:

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:

function ConfirmSettingEnddateRemarkToYesterday(remarkID) {

    //Some code...

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

}

注释掉你可以看到一个警报为我自己和2尝试刷新视图。该location.reload()的作品,但基本上是针对浏览器的工作太多了。将.html(结果)员额整个index.cshtml + Layout.cshtml双在remarksgrid股利。所以这是不正确的。

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.

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

This is the action it calls (SetEnddateRemarkToYesterday):

       public ActionResult SetEnddateRemarkToYesterday(int remarkID) {

        //Some logic to persist the change to DB.

        return RedirectToAction("Index");
    }

这是它重定向到行动:

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

}

如果我不这样做window.location.reload的succesfull AJAX发布后视图永远不会重新加载。我是新来的MVC,但我敢肯定有更好的方法来做到这一点。我不理解的东西在这里根本。也许在一个正确的方向轻推?谢谢你在前进。

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调用,你应该使用重定向其响应

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

修改您的控制器与登陆网址返回JSONResult:

Modify your controller to return JSONResult with landing url:

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调用:

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

这篇关于AJAX后视图后不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:17