0

我是 asp.net 的新手,我使用 $.post(Jquery 语法)将我的所有数据发布到服务器。

在完成动作方法执行后,控制器的动作被执行@结束我将 RedirectToAction 调用到不同的动作方法。

Eecution 到达 $.Post() 的完成回调事件,其中我将 html 中的请求结果加载到页面的根元素。$(html).html(结果)。

我如何将 $.post 与 RedirectToAction 一起使用

4

1 回答 1

2

替换整个页面时使用 AJAX 有什么意义?AJAX 的全部意义在于只刷新 DOM 的特定部分。如果您要刷新整个页面,则只需使用标准链接,无需 AJAX。但是,如果我们假设您的控制器操作处理 2 种情况:一种是返回局部视图,另一种是重定向,您可以将目标 url 作为 JSON 传递:

[HttpPost]
public ActionResult SomeAction()
{
    if (Something)
    {
        return PartialView();
    }

    return Json(new { redirectTo = Url.Action("Foo", "Bar") });
}

然后在客户端:

$.post('@Url.Action("SomeAction")', function(result) {
    if (result.redirectTo) {
        // the controller action passed us the url to redirect to
        window.location.href = result.redirectTo;
    } else {
        // the controller action passed us a partial result => 
        // let's update some portion of the DOM
        $('#someId').html(result);
    }
});
于 2012-07-30T15:54:33.200 回答