为什么批准索引操作没有重定向到挂起的操作视图,尽管调试代码没有错误?

why approval index action not redirect to pending action view although no error on debug code?

提问人:ahmed abdelaziz 提问时间:10/18/2023 最后编辑:marc_sahmed abdelaziz 更新时间:10/20/2023 访问量:34

问:

我从事一个 ASP.NET MVC 项目。我遇到问题操作没有重定向到操作,尽管没有发生错误。ApprovalIndexPendingManagersRequests

我调试并跟踪断点,直到到达操作,并跟踪直到我到达查看,没有任何问题。PendingManagersRequestsreturn View(vmr);

那么,为什么它没有重定向到视图,尽管没有发生任何问题呢?PendingManagersRequests

我的代码:当点击按钮批准提交时,它会根据以下条件更新表列:SpeakStuffCommentRequestNo

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>
}

当单击批准按钮时,它会调用控制器:ApprovalIndexResignationController

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在控制器上调用操作:approvalIndexresignation

    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

error local host say

jquery ajax asp.net-web-api C#-7.3

评论

0赞 steve v 10/18/2023
当您在浏览器中打开开发人员工具并查看网络请求时,调用返回重定向状态结果还是 200 状态结果?ApprovalIndex
0赞 ahmed abdelaziz 10/18/2023
你能告诉我如何查看开发工具请求以及如何查看它是 200 OK or not 你能告诉我吗
0赞 ahmed abdelaziz 10/18/2023
你能帮帮我吗

答:

1赞 Eduardo Molteni 10/20/2023 #1

当您通过 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);
            }
        });
    }