我正在尝试将带有序列化形式的数组作为参数从ajax发送到控制器,但第二个参数“test”是给出空值
$("#CreateDepartmentForm").submit(function (e) { e.preventDefault();
var modelx = $(this).serialize();
console.log(modelx);
var cars = [
{
"color": "purple",
"type": "minivan",
"registration": "2018-03-03",
"capacity": 7
},
{
"color": "red",
"type": "station wagon",
"registration": "2018-03-03",
"capacity": 20
}];
$.ajax({
type: "POST",
url: "/Department/CreateDepartmentByAjax",
data: modelx + "&test=" + cars,
success: function (res) {
},
});
public IActionResult CreateDepartmentByAjax(DepartmentVM model,List<cars> test)
{
}
public class cars
{
public string color { get; set; }
public string type { get; set; }
public string registration { get; set; }
public int capacity { get; set; }
}
-----------
我正在尝试将具有序列化形式的数组作为参数从ajax发送到控制器,但第二个参数“test”给出空值 所以,问题是控制器没有得到参数测试,我尝试了很多示例和教程,但没有任何帮助。也许我遗漏了一些简单明显的错误?如何将参数从 ajax 函数传递到控制器?
$("#CreateDepartmentForm").submit(function (e) {
e.preventDefault();
var modelx = $(this).serialize();
console.log(modelx);
var cars = [
{
"color": "purple",
"type": "minivan",
"registration": "2018-03-03",
"capacity": 7
},
{
"color": "red",
"type": "station wagon",
"registration": "2018-03-03",
"capacity": 20
}];
$.ajax({
type: "POST",
url: "/Department/CreateDepartmentByAjax",
data: modelx + "&test=" + cars,
success: function (res) {
},
});
public IActionResult CreateDepartmentByAjax(DepartmentVM model,List<cars> test)
{
}
public class cars
{
public string color { get; set; }
public string type { get; set; }
public string registration { get; set; }
public int capacity { get; set; }
}
-----------
I'm trying To Send Array with Serialize Form as a parameter from ajax to controller but the second parameter "test" giving null value
So, the problem is that controller doesn't get the parameter test, I've tried many examples and tutorials but nothing helped. maybe there's some easy obvious mistake which I am missing? How could I pass parameters from ajax function to controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过将对象序列化为 JSON 将对象传递给 API。
并修改控制器:
Pass the object to API by serializing the object as JSON.
And modify the controller:
如果您想为 POST 请求传递类似数组的查询参数,您需要在控制器方法中添加 [FromQuery] 属性来列出参数。相反,最好将 json 有效负载放入请求正文中。
If you want to pass an array like query parameter for POST request you need to add [FromQuery] attribute to list parameter in you controller method. Instead it's better to put json payload to request body.