Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用AJAX将对象的JSON数组传递给控制器_Jquery_.net_Json_Ajax_Model View Controller - Fatal编程技术网

Jquery 使用AJAX将对象的JSON数组传递给控制器

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

我正在尝试将一个对象数组传递给控制器,其中的值由表单发布。每次我通过数组时,它都会在控制器的操作中给我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
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 })
});