为什么进入控制器的参数为 0?

发布于 2025-01-15 04:07:25 字数 1381 浏览 0 评论 0原文

我有一个 .NET Core 应用程序,我使用 ajax 访问控制器,传递两个参数,然后打开视图。当我通过 ajax 调用传递一个参数时,一切正常,并且控制器接收到该参数作为正确的值。当我传递两个参数时,两个参数都是 0。当它们通过 ajax 调用提交时,它们不是 0。 ajax 调用和将两个参数转换为 0 的控制器之间发生了一些事情。我不确定我做错了什么。

Ajax CAll:

id 和 orderId 均在此处显示为正确值。

 if (id != undefined) {
        id = Number(id);
    } else {
        id = 0;
    };

    var orderId = Number(document.getElementById('orderId').value);

    var data = JSON.stringify({       
        'id': id,
        'orderId': orderId       
    });
   
    $.ajax({
        url: "/AddorEditTask/OpenTaskForm",
        type: 'POST',
        async: false,
        dataType: 'json',
        contentType: 'application/json',
        data: { id: id, orderId: orderId },
        error: function (e) {
            alert("Unable to open task form. " + e.responseText);
            console.log(e.responseText);
        }
    }).done(function (data) {
        //alert(data.newUrl);
        window.location.replace(data.newUrl);
    });

控制器:

id 和 orderId 在这里都显示为 0。

[HttpPost] //Being used in AJAX call to all the "AddorEdit" GET action and open the task form.
    public ActionResult OpenTaskForm(int id,int orderId)
    {
        return Json(new
        {
            newUrl = Url.Action("AddorEditTask", new { id = id, orderId = orderId })
        }
        );
    }

提前致谢!

I have a .NET Core app and I am using ajax to access a controller, pass two parameters, and then open the view. When I pass one parameter via my ajax call everything works fine and the parameter is received by the controller as the right value. When I pass two both parameters are 0. They are not 0 when they are submitted via the ajax call. Something happens between the ajax call and the controller that converts both parameters to 0. I'm not sure what I am doing wrong.

Ajax CAll:

id and orderId both show up here as the correct values.

 if (id != undefined) {
        id = Number(id);
    } else {
        id = 0;
    };

    var orderId = Number(document.getElementById('orderId').value);

    var data = JSON.stringify({       
        'id': id,
        'orderId': orderId       
    });
   
    $.ajax({
        url: "/AddorEditTask/OpenTaskForm",
        type: 'POST',
        async: false,
        dataType: 'json',
        contentType: 'application/json',
        data: { id: id, orderId: orderId },
        error: function (e) {
            alert("Unable to open task form. " + e.responseText);
            console.log(e.responseText);
        }
    }).done(function (data) {
        //alert(data.newUrl);
        window.location.replace(data.newUrl);
    });

Controller:

id and orderId both show up here as 0.

[HttpPost] //Being used in AJAX call to all the "AddorEdit" GET action and open the task form.
    public ActionResult OpenTaskForm(int id,int orderId)
    {
        return Json(new
        {
            newUrl = Url.Action("AddorEditTask", new { id = id, orderId = orderId })
        }
        );
    }

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2025-01-22 04:07:26

我想通了这个问题。我发出了“发布”请求而不是“获取”请求,这导致了问题。我更新了我的控制器,如下所示:

[HttpGet] //Being used in AJAX call to all the "AddorEdit" GET action and open the task form.
    public ActionResult OpenTaskForm(int id,int orderId)
    {
        return Json(new
        {
            newUrl = Url.Action("AddorEditTask", new { id = id, orderId = orderId })
        }
        );
    }

我的 ajax 调用如下所示:

$.ajax({
    url: "/AddorEditTask/OpenTaskForm",
    type: 'GET',    
    data: { "id": id, "orderId": orderId },
    error: function (e) {
        alert("Unable to open task form. " + e.responseText);
        console.log(e.responseText);
    }
}).done(function (data) {
    //alert(data.newUrl);
    window.location.replace(data.newUrl);
});

I figured out the issue. I was making a 'post' request instead of a 'get' request and that was causing the issue. I updated my controller to look like:

[HttpGet] //Being used in AJAX call to all the "AddorEdit" GET action and open the task form.
    public ActionResult OpenTaskForm(int id,int orderId)
    {
        return Json(new
        {
            newUrl = Url.Action("AddorEditTask", new { id = id, orderId = orderId })
        }
        );
    }

My ajax call looks like:

$.ajax({
    url: "/AddorEditTask/OpenTaskForm",
    type: 'GET',    
    data: { "id": id, "orderId": orderId },
    error: function (e) {
        alert("Unable to open task form. " + e.responseText);
        console.log(e.responseText);
    }
}).done(function (data) {
    //alert(data.newUrl);
    window.location.replace(data.newUrl);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文