我从事 ASP.NET MVC 项目。我面临问题,操作
ApprovalIndex
没有重定向到操作 PendingManagersRequests
,尽管没有发生错误。
我调试和跟踪断点,直到达到操作
PendingManagersRequests
并跟踪,直到我到达查看 return View(vmr);
,没有任何问题。
那么,尽管没有发生任何问题,为什么它没有重定向到视图
PendingManagersRequests
?
我的代码:当单击按钮批准提交时,它会根据
SpeakStuffComment
更新表格列RequestNo
:
using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" }))
{
@Html.AntiForgeryToken()
<a onclick="submit();" class="btn btn-primary" style="min-width: 100px;
margin-left: 5px;">
<i class="glyphicon glyphicon-ok"></i> Approve
</a>
}
当点击批准按钮时,它会调用控制器上的
ApprovalIndex
ResignationController
:
public class ResignationController : Controller
{
[HttpPost]
public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
{
string errorMsg = string.Empty;
string requestStatus;
Workforce.ResignationUpdateLineManangerApproval(id, true,Convert.ToInt32(Session[SessionKeys.UserCode]));
return RedirectToAction("PendingManagersRequests", new { msg = $"Request NO {REQ.RequestNo} has been accepted " +
$"successfully." });
}
}
jQuery 在
approvalIndex
控制器上调用操作 resignation
:
function submit() {
var ResignationRequester = new Object();
ResignationRequester.RequestNo = document.getElementById("RequestNo").innerHTML.trim();
ResignationRequester.EmpID = document.getElementById("EmpID").innerHTML.trim();
ResignationRequester.SpeakStuffComment = document.getElementById("SpeakStuffComment").value;
if (ResignationRequester != null) {
$.ajax({
type: "POST",
url: '@Url.Action("ApprovalIndex", "Resignation")',
data: JSON.stringify(ResignationRequester),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
}
</script>
public async Task<ActionResult> PendingManagersRequests(string msg, string errorMsg)
{
ViewModelRequests vmr = new ViewModelRequests();
vmr.MyRequests = Workforce.GetPendingToDisplayMyRequests(Session[SessionKeys.UserCode].ToString());
ViewBag.msg = msg;
ViewBag.errorMsg = errorMsg;
return View(vmr);
}
最后我只得到这个弹出本地主机说虽然从行动
approvalindex
没有错误
当您通过 ajax 调用服务器时,重定向不起作用。
您必须在客户端“重定向”(这不是真正的重定向):
if (ResignationRequester != null) {
$.ajax({
type: "POST",
url: '@Url.Action("ApprovalIndex", "Resignation")',
data: JSON.stringify(ResignationRequester),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log(response);
window.location = "/Home/PendingManagersRequests?msg=something";
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}