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

eqoofvh9  于 2023-11-17  发布在  jQuery
关注(0)|答案(1)|浏览(92)

我在一个ASP.NET MVC项目上工作。我面临的问题是,尽管没有错误发生,但操作ApprovalIndex没有重定向到操作PendingManagersRequests
我调试和跟踪断点,直到达到动作PendingManagersRequests,并跟踪,直到我达到查看return View(vmr);没有任何问题。
那么,为什么没有重定向到视图PendingManagersRequests,尽管没有问题发生?
我的代码:当单击按钮approve submit时,它会根据RequestNo更新表列SpeakStuffComment

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

字符串
当单击approve按钮时,它会调用控制器ResignationController上的ApprovalIndex

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在resignation控制器上调用action approvalIndex

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没有错误


的数据

x3naxklr

x3naxklr1#

当你通过匿名调用服务器时,重定向不起作用。
你必须“重定向”(这不是真正的重定向)客户端:

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

字符串

相关问题