我正在尝试使用序列化形式将数组作为参数从 ajax 发送到控制器,但第二个参数 "test" 给出空值
I'm trying To Send Array with Serialize Form as a parameter from ajax to controller but the second parameter "test" giving null value
$("#CreateDepartmentForm").提交(函数(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 Code Image`
------------
我正在尝试将带有序列化形式的数组作为参数从 ajax 发送到控制器,但第二个参数“test”给出空值
所以,问题是控制器没有得到参数测试,我试过很多例子和教程,但没有任何帮助。也许我遗漏了一些明显的错误?如何将参数从 ajax 函数传递给控制器?
通过将对象序列化为 JSON 将对象传递给 API。
$("#CreateDepartmentForm").submit(function (e) {
e.preventDefault();
var modelx = {};
$(this).children("input").each(function () {
var name = $(this).attr('name');
var value = $(this).val();
modelx[name] = value;
});
...
$.ajax({
type: "POST",
url: "/Department/CreateDepartmentByAjax",
contentType: "application/json",
data: JSON.stringify({ model: modelx, test: cars }),
success: function (res) {
},
});
});
并修改控制器:
public class CreateDepartmentInput
{
public DepartmentVM model { get; set; }
public List<cars> test { get; set; }
}
[HttpPost]
public IActionResult CreateDepartmentByAjax([FromBody]CreateDepartmentInput input)
{
...
}
Request Payload
Visual Studio Debugging Output
如果您想为 POST 请求传递类似查询参数的数组,您需要将 [FromQuery] 属性添加到控制器方法中的列表参数。相反,最好将 json 有效负载放入请求正文。
$("#CreateDepartmentForm").提交(函数(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 Code Image`
------------
我正在尝试将带有序列化形式的数组作为参数从 ajax 发送到控制器,但第二个参数“test”给出空值 所以,问题是控制器没有得到参数测试,我试过很多例子和教程,但没有任何帮助。也许我遗漏了一些明显的错误?如何将参数从 ajax 函数传递给控制器?
通过将对象序列化为 JSON 将对象传递给 API。
$("#CreateDepartmentForm").submit(function (e) {
e.preventDefault();
var modelx = {};
$(this).children("input").each(function () {
var name = $(this).attr('name');
var value = $(this).val();
modelx[name] = value;
});
...
$.ajax({
type: "POST",
url: "/Department/CreateDepartmentByAjax",
contentType: "application/json",
data: JSON.stringify({ model: modelx, test: cars }),
success: function (res) {
},
});
});
并修改控制器:
public class CreateDepartmentInput
{
public DepartmentVM model { get; set; }
public List<cars> test { get; set; }
}
[HttpPost]
public IActionResult CreateDepartmentByAjax([FromBody]CreateDepartmentInput input)
{
...
}
Request Payload
Visual Studio Debugging Output
如果您想为 POST 请求传递类似查询参数的数组,您需要将 [FromQuery] 属性添加到控制器方法中的列表参数。相反,最好将 json 有效负载放入请求正文。