为什么进入控制器的参数为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了这个问题。我发出了“发布”请求而不是“获取”请求,这导致了问题。我更新了我的控制器,如下所示:
我的 ajax 调用如下所示:
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:
My ajax call looks like: