Javascript ASP.NET MVC局部视图ajax帖子?

Javascript ASP.NET MVC局部视图ajax帖子?,javascript,jquery,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Javascript,Jquery,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Index.html(查看) 问题:如果操作成功,我希望重定向到另一个页面(类别)。但没有操作,没有错误消息。如果手术不成功,它会像我预期的那样工作 我该怎么做?如何使用AJAX post路由另一个页面?您所做的AJAX调用不应该能够重定向整个页面。它只向异步调用返回数据。如果要执行重定向,请执行以下操作: 重定向的javascript方法是使用window.location 因此,您的ajax调用应该如下所示: <script> $(document).ready(func

Index.html(查看)

问题:如果操作成功,我希望重定向到另一个页面(类别)。但没有操作,没有错误消息。如果手术不成功,它会像我预期的那样工作


我该怎么做?如何使用AJAX post路由另一个页面?

您所做的AJAX调用不应该能够重定向整个页面。它只向异步调用返回数据。如果要执行重定向,请执行以下操作:

重定向的javascript方法是使用window.location

因此,您的ajax调用应该如下所示:

<script>
    $(document).ready(function () {
        $('input[type=submit]').click(function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: '@Url.Action("_AddCategory", "Categories")',
                dataType: "json",
                data: $('form').serialize(),
                success: function (result) {
                    window.location='@Url.Action("Categories")';
                },
                error: function () {

                }
            });
        });
    });
</script>

不要重定向使用AJAX调用的控制器操作。没用。您可以将要重定向到的url作为JsonResult返回:

[HttpPost]
public ActionResult _AddCategory(CategoriesViewModel viewModel)
{
    if(//success)
    {
        // DbOperations...
        return Json(new { redirectTo = Url.Action("Categories") });
    }
    else
    {
        // model state is not valid...
        return PartialView(viewModel);
    }
}
然后在客户端测试此url是否存在,并采取相应措施:

$.ajax({
    type: "POST",
    url: '@Url.Action("_AddCategory", "Categories")',
    data: $('form').serialize(),
    success: function (result) {
        if (result.redirectTo) { 
            // The operation was a success on the server as it returned
            // a JSON objet with an url property pointing to the location
            // you would like to redirect to => now use the window.location.href
            // property to redirect the client to this location
            window.location.href = result.redirectTo;
        } else {
            // The server returned a partial view => let's refresh
            // the corresponding section of our DOM with it
            $(".categories_content_container").html(result);
        }
    },
    error: function () {

    }
});

还要注意,我已经从
$.ajax()
调用中去掉了
数据类型:“json”
参数。这一点非常重要,因为我们并不总是返回JSON(在您的情况下,您从未返回JSON,因此此参数绝对错误)。在我的示例中,我们仅在成功的情况下返回JSON,在失败的情况下返回
text/html
(PartialView)。因此,您应该离开jQuery,只需使用服务器返回的
Content-Type
HTTP响应头自动推断类型,并相应地解析传递给成功回调的结果参数。

您是否已经尝试添加重定向?你的问题暗示是的,但我在你的代码中看不到类似的功能。OIC,你试图在你的action@DaveA我指的是控制器的成功,而不是ajax。我添加
返回重定向到操作(“类别”)以重定向。如果模型状态无效,则ajax post成功(这是预期的)。我想在控制器操作中重定向,这可能吗?否则,我如何在ajax成功中捕捉到这一点并重定向到另一个页面。总之,如果DbOperations成功,我想重定向另一个页面,否则再次加载带有模型的partialView。那么,我应该返回“true或false”而不是“partialView或RedirectToAction”+1吗。非常感谢,但是@DarinDimitrov的答案是我想要的。我有同样的问题,因为数据类型:“json”。谢谢你,达林!感谢您提供的简单代码示例。我能够为我的自定义实例非常有效地修改它。。。但代码的清晰性是关键。谢谢
<script>
    $(document).ready(function () {
        $('input[type=submit]').click(function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: '@Url.Action("_AddCategory", "Categories")',
                dataType: "json",
                data: $('form').serialize(),
                success: function (result) {
                    window.location='@Url.Action("Categories")';
                },
                error: function () {

                }
            });
        });
    });
</script>
public ActionResult _AddCategory(CategoriesViewModel viewModel)
{
    if(//success)
    {
        return Json(true);
    }
    else
    {
        return Json(false);
    }
}
[HttpPost]
public ActionResult _AddCategory(CategoriesViewModel viewModel)
{
    if(//success)
    {
        // DbOperations...
        return Json(new { redirectTo = Url.Action("Categories") });
    }
    else
    {
        // model state is not valid...
        return PartialView(viewModel);
    }
}
$.ajax({
    type: "POST",
    url: '@Url.Action("_AddCategory", "Categories")',
    data: $('form').serialize(),
    success: function (result) {
        if (result.redirectTo) { 
            // The operation was a success on the server as it returned
            // a JSON objet with an url property pointing to the location
            // you would like to redirect to => now use the window.location.href
            // property to redirect the client to this location
            window.location.href = result.redirectTo;
        } else {
            // The server returned a partial view => let's refresh
            // the corresponding section of our DOM with it
            $(".categories_content_container").html(result);
        }
    },
    error: function () {

    }
});