将整数列表从JavaScript传递到C# - 我得到了列表,但它是空的; 表单数据结构不正确?

Sea*_*son 6 javascript c# json

我的数组填充如下:

updateLabels: function () {
    var diagrams = _stage.diagramLayer.getChildren();

    var componentIDs = new Array();

    for (var index = 0; index < diagrams.length; index++) {
        componentIDs.push(diagrams[index].componentID);
    }

    var self = this;
    $.ajax({
        url: '../PlanView/UpdateDiagrams',
        type: 'POST',
        data: { ComponentIDs: JSON.stringify(componentIDs), RackInfo: $('#RackInfoSelect').val() },
        success: function (data) {
            console.log('success');
        },
        error: function () {
            console.log("error");
        }
    });
    },
Run Code Online (Sandbox Code Playgroud)

服务器端我有这个方法:

[CompressionFilterAttribute]
public JsonResult UpdateDiagrams(List<int> componentIDs, string rackInfo)
{
    List<object> diagramInformation = new List<object>(componentIDs.Count());
}
Run Code Online (Sandbox Code Playgroud)

我的数据正在通过网络传递:

ComponentIDs:[74,445,732,351,348,347,1123,599,600,1053,350,601,602,603,332,99,877,919,349,348,347,347,349,348]
RackInfo:Equipment Weight
Run Code Online (Sandbox Code Playgroud)

我成功获得RackInfo,如果我将UpdateDiagrams更改为期望List<string>然后我得到一个包含一个项目的列表,整个ComponentIDs字符串.

我在这里做错了什么?

编辑:我在MVC3下工作.我应该能够在传递给我的控制器时利用某种自动反序列化,我只是不确定如何.

解决方案:解决方案是将我的数据对象包装在JSON.stringify中,而不仅仅是componentIDs.即使我可以获得RackInfo变量服务器端而不将其转换为JSON.

Luk*_*keH 4

如果您希望发布的数据采用 JSON 格式,请尝试这样的操作。然后,MVC 应该能够在服务器端自动反序列化它。

$.ajax({
    url: '../PlanView/UpdateDiagrams',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        componentIDs: componentIDs,
        rackInfo: $('#RackInfoSelect').val()
    }),
    success: function (data) {
        console.log('success');
    },
    error: function () {
        console.log("error");
    }
});
Run Code Online (Sandbox Code Playgroud)

(我目前无法测试它,但即使它不是完全没有错误,它也应该是正确的。)