Jquery 使用AJAX将对象的JSON数组传递给控制器
我正在尝试将一个对象数组传递给控制器,其中的值由表单发布。每次我通过数组时,它都会在控制器的操作中给我null 下面是名为Itemspecs的对象数组Jquery 使用AJAX将对象的JSON数组传递给控制器,jquery,.net,json,ajax,model-view-controller,Jquery,.net,Json,Ajax,Model View Controller,我正在尝试将一个对象数组传递给控制器,其中的值由表单发布。每次我通过数组时,它都会在控制器的操作中给我null 下面是名为Itemspecs的对象数组 [object Array]: [Object, Object, Object] 0: Object ItemID: 0 PropertyID: 7 Value: "hj" ValueID: 0 1: Object ItemID: 0 PropertyID: 8 Value: "Red" ValueID: 0 2: Object ItemI
[object Array]: [Object, Object, Object]
0: Object
ItemID: 0
PropertyID: 7
Value: "hj"
ValueID: 0
1: Object
ItemID: 0
PropertyID: 8
Value: "Red"
ValueID: 0
2: Object
ItemID: 0
PropertyID: 19
Value: "jh"
ValueID: 0
jQuery
jQuery.ajaxSettings.traditional = true
$.ajax({
type: 'POST',
cache: false,
url: '/Item/AddOrEdit',
contentType: 'application/json; charset=utf-8',
dataType:'json',
//data: $(form).serialize()
data: JSON.stringify({ form, Itemspecs })
});
我在数据中传递两个参数。参数form
是来自MVC表单的值,Itempecs1
是我的数组
public ActionResult AddOrEdit(List<ValueViewModel> Itemspecs, ItemViewModel form)
{
// Save posted data
}
这就是我通过组合其他两个数组来制作这个itemspecs
数组的方法
var Value = [];
$('#SpecsPlaceHolder :input').each(function () {
Value.push($(this).val());
});
console.log(Value + ": These Are Values");
var PropertyID = [];
$('#SpecsPlaceHolder :input').each(function (index) {
// For debugging purposes...
//alert(index + ': ' + $(this).attr('id'));
PropertyID.push($(this).data("id"));
//PropertyID.push($(this).attr('id'));
});
console.log(PropertyID + ": These Are Ids");
var Itemspecs = [];
$.each(PropertyID, function (index, value) {
Itemspecs.push({ 'ValueID': 0, 'Value': Value[index], 'PropertyID': value, 'ItemID': 0, });
});
Value=(3)[“gh”、“Red”、“hg”]
和PropertyID=(3)[7,8,19]
然后将它们组合到itemspecs,后者在控制台中返回如下对象数组:
(3) [{…}, {…}, {…}]
0:{ValueID: 0, Value: "gh", PropertyID: 7, ItemID: 0}
1:{ValueID: 0, Value: "Red", PropertyID: 8, ItemID: 0}
2:{ValueID: 0, Value: "hg", PropertyID: 19, ItemID: 0}
length:3
__proto__:Array(0)
因此,我所做的是在当前Ajax的成功中调用另一个Ajax,并在第一个Ajax中发送表单,在第二个Ajax中发送数组
$.ajax({
type: 'POST',
url: form.action,
data: $(form).serialize(),
success: function (response) {
if (response.success && response.data > 0)
{
debugger
var Itemspecs = [];
$.each(PropertyID, function (index, value) {
Itemspecs.push({ 'ValueID': 0, 'Value': Value[index], 'PropertyID': value, 'ItemID': response.data });
});
console.log(Itemspecs);
$.ajax({
type: 'POST',
url: '@Url.Action("PostData", "Item")/',
cache: false,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(Itemspecs),
success: function (response) {
if (response.success) {
Popup.dialog('close');
dataTable.ajax.reload();
$.notify(response.message, {
globalPosition: "top center",
className: "success"
})
}
}
});
}
}
});
发布的JSON缺少
表单
日期,您的JSON应该具有ItemViewModel表单
的表单
,以及ItemViewModel表单
的Itemspecs
,具有相同的模型属性命名约定
您的JSON应该如下所示
{
"form": {
"status": "0001",
"date": "0002"
},
"Itemspecs": [
{
"ItemID": 0,
"PropertyID": 7,
"Value": "hj",
"ValueID": 0
},
{
"ItemID": 0,
"PropertyID": 8,
"Value": "Red",
"ValueID": 0
}
]
}
使用下面的JS将填充缺少的表单
数据,以便您可以在控制器中接收它
// sample data for test
var Itemspecs =
[
{ "ItemID": 0, "PropertyID": 7, "Value": "hj", "ValueID": 0 },
{ "ItemID": 0, "PropertyID": 7, "Value": "hj", "ValueID": 0 }
];
var form = {};
// loop throw form and get all data
$.each($('#form1').serializeArray(), function (i, field) {
form[field.name] = field.value || '';
});
jQuery.ajaxSettings.traditional = true
$.ajax({
type: 'POST',
cache: false,
url: '/Item/AddOrEdit',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify({ form, Itemspecs })
});
// sample data for test
var Itemspecs =
[
{ "ItemID": 0, "PropertyID": 7, "Value": "hj", "ValueID": 0 },
{ "ItemID": 0, "PropertyID": 7, "Value": "hj", "ValueID": 0 }
];
var form = {};
// loop throw form and get all data
$.each($('#form1').serializeArray(), function (i, field) {
form[field.name] = field.value || '';
});
jQuery.ajaxSettings.traditional = true
$.ajax({
type: 'POST',
cache: false,
url: '/Item/AddOrEdit',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify({ form, Itemspecs })
});