Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过javascript向控制器发布的行为与直接从submit按钮发布的行为不同_Javascript_Jquery_Asp.net Mvc - Fatal编程技术网

通过javascript向控制器发布的行为与直接从submit按钮发布的行为不同

通过javascript向控制器发布的行为与直接从submit按钮发布的行为不同,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我有一个表单中的按钮: <button id="save" type="submit" formaction="@Url.Action("SaveEvent")" formmethod="post">Save</button> 我尝试在JS函数中重定向,但它没有返回新保存的值: window.location = '@Html.Raw(@Url.Action("EventDetail", new { eventId = @Model.EventId}))'; 有人能解

我有一个表单中的按钮:

<button id="save" type="submit" formaction="@Url.Action("SaveEvent")" formmethod="post">Save</button>
我尝试在JS函数中重定向,但它没有返回新保存的值:

window.location = '@Html.Raw(@Url.Action("EventDetail", new { eventId = @Model.EventId}))';

有人能解释一下为什么从JS发帖时控制器中的重定向不起作用吗?

控制器中与重定向相关的方法(
redirect
RedirectToAction
等)返回302响应,其中要重定向到的新url作为
位置
头的值,浏览器通常会向该头发出新的GET请求

您不应该调用从ajax调用返回302响应的action方法。相反,您应该向发出请求的ajax调用返回json响应

public async Task<ActionResult> SaveEventAsync(EventDataModel vm)
{
    // save to db etc...  
    return Json(new {status = "success"});    
}

我更新了您的代码以使用Url.Action方法来构建操作方法的正确相对Url。如果您的javascript代码位于单独的js文件中,请在razor页面中构建基本url,并按本文所述传递给您的js代码。

从未听说过
RedirectToActionWithReturnUrl
方法!这是你的习惯方法吗?它的作用是什么?这可能是因为您的SaveEventAsync需要一个EventDataModel类型的对象,而您没有将该对象传递给它在ajax请求中不能返回
RedirectResult
。是的,ReturnToActionWithReturnUrl是一个自定义方法,它只向routeValue添加一个URL。不知道为什么,因为我没有写:-)。我现在明白了重定向结果不能返回到ajax请求。感谢Shyju提供的代码示例。很好。谢谢你的解释和样品。我的代码可以很好地处理这个问题。
window.location = '@Html.Raw(@Url.Action("EventDetail", new { eventId = @Model.EventId}))';
public async Task<ActionResult> SaveEventAsync(EventDataModel vm)
{
    // save to db etc...  
    return Json(new {status = "success"});    
}
$('#save').click(function(e){

    e.preventDefault();

    var url="@Url.Action("SaveEvent","YourControllerName")";

    $.post(url, { EventID: "@Model.EventId", SomeValue: 123 }, function(res){
        if (res.status === "success") {
           window.location.href =" @Url.Action("EventDetail","YourControllerName",
                                                          new { eventId=Model.EventId })";
        }
    });

});