简体   繁体   English

JSON回传后,MVC RedirectToAction不起作用

[英]MVC RedirectToAction Doesn't Work After JSON Post Return

I am trying to change the page after post process of the AJAX process which executes by MVC. 我正在尝试在由MVC执行的AJAX流程的后期处理之后更改页面。 I have used it different way maybe my usage might be wrong. 我以不同的方式使用它,也许我的用法可能是错误的。

C# MVC code part. C#MVC代码部分。 I am sending int list which is user list and process and do something. 我正在发送int列表,它是用户列表和进程并执行某些操作。

[HttpPost]
public ActionResult SelectUserPost(int[] usersListArray)
{
    // lots of code but omitted

    return JavaScript("window.location = '" + Url.Action("Index", "Courses") + "'"); // this does not work
    return RedirectToAction("Index"); // this also does not            
    return RedirectToAction("Index","Courses"); // this also does not
}

My problem is redirect part do not work after the MVC process ends. 我的问题是MVC流程结束后,重定向部分不起作用。 Process works, only redirect doesn't. 流程有效,只有重定向无效。

JavaScript code here JavaScript代码在这里

// Handle form submission event
$('#mySubmit').on('click',
    function(e) {
        var array = [];
        var rows = table.rows('.selected').data();
        for (var i = 0; i < rows.length; i++) {
            array.push(rows[i].DT_RowId);
        }

        // if array is empty, error pop box warns user
        if (array.length === 0) {
            alert("Please select some student first.");
        } else {
            var courseId = $('#userTable').find('tbody').attr('id');

            // push the id of course inside the array and use it
            array.push(courseId);
            $.ajax({
                url: "/Courses/SelectUserPost",
                type: "POST",
                data: JSON.stringify(array),
                dataType: "json",
                contentType: 'application/json; charset=utf-8'
            });
        }
    });

Added this to AJAX and it is not working too 将此添加到AJAX,它也无法正常工作

success: function() {
  window.location.href = "@Url.Content("~/Courses/Index")";
}

Once you are using AJAX the browser is unaware of the response. 使用AJAX后,浏览器将不会意识到响应。

The AJAX success in its current form failed because redirect response code is not in the 2xx status but 3xx 当前格式的AJAX success失败,因为重定向响应代码不是处于2xx状态而是3xx

You would need to check the actual response and perform the redirect manually based on the location sent in the redirect response. 您将需要检查实际响应并根据重定向响应中发送的位置手动执行重定向。

//...
success: function(response) { 
    if (response.redirect) {
        window.location.href = response.redirect;
    } else {
        //...
    }
}

//...

Update 更新资料

Working part for anyone who need asap: 需要任何人尽快工作的部分:

Controller Part: 控制器部分:

 return RedirectToAction("Index","Courses");

Html part: HTML部分:

$.ajax({
        url: "/Courses/SelectUserPost",
        type: "POST",
        data: JSON.stringify(array),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert("Successful!");
            window.location.href = "@Url.Content("~/Courses/Index")";
        }
    });

Just deleted 刚删除

dataType: 'json' dataType:'json'

Part because I am using my own data type instead of JSON. 部分原因是我使用自己的数据类型而不是JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM