【问题标题】:Is it possible to use $.post with RedirectToAction in mvc是否可以在 mvc 中使用 $.post 和 RedirectToAction
【发布时间】:2012-07-30 23:54:33
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    替换整个页面时使用 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);
        }
    });
    

    【讨论】:

    • 我们可以在 if 块中摆脱 Window.Location.href,是否已经完成了在 Jquery 中启动的回调方法,我的问题是我们不能使用 RedirectToAction 使用 $ 重定向到不同的视图。发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    相关资源
    最近更新 更多