[英]MVC RedirectToAction Doesn't Work After JSON Post Return
我正在尝试在由MVC执行的AJAX流程的后期处理之后更改页面。 我以不同的方式使用它,也许我的用法可能是错误的。
C#MVC代码部分。 我正在发送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
}
我的问题是MVC流程结束后,重定向部分不起作用。 流程有效,只有重定向无效。
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'
});
}
});
将此添加到AJAX,它也无法正常工作
success: function() {
window.location.href = "@Url.Content("~/Courses/Index")";
}
使用AJAX后,浏览器将不会意识到响应。
当前格式的AJAX success
失败,因为重定向响应代码不是处于2xx
状态而是3xx
您将需要检查实际响应并根据重定向响应中发送的位置手动执行重定向。
//...
success: function(response) {
if (response.redirect) {
window.location.href = response.redirect;
} else {
//...
}
}
//...
需要任何人尽快工作的部分:
控制器部分:
return RedirectToAction("Index","Courses");
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")";
}
});
刚删除
dataType:'json'
部分原因是我使用自己的数据类型而不是JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.