1

I have the following json object, sorry for the image;

enter image description here

The jquery code I have looks like this;

var data = {
    table: table,
    favour: $("[name='radFavour']:checked").val(),
    data: jsonObj
};

$.ajax({
    url: appDomain + "/Compare/Ajax_Update",
    type: "post",
    dataType: "json",
    data: data
});

The c# code looks like;

[HttpPost]
public void Ajax_Update(CompareFVM fvm)
{
}

The FVM contains a string for table and for favour and the data for those two properties comes through.

For "data" I have the following in the FVM;

public List<CompareItem> data { get; set; }

And the item;

public class CompareItem
{
    public int prodId { get; set; }
    public int stageId { get; set; }
    public string value { get; set; }
    public string property { get; set; }
}

The List has the correct amount of elements in it, in this case two, but each of them has nulls set.

So the data I am posting back is not coming through for the array elements but it is for the single fields.

Any ideas?

4

2 回答 2

1

在 ajax 调用时,将对象名作为“fvm”传递(名称应与 C# 代码参数匹配)。另外,请检查使用 JSON.stringify(data) 传递 json abject。

    var fvm = {
        table: table,
        favour: $("[name='radFavour']:checked").val(),
        data: jsonObj
    };

    $.ajax({
        url: appDomain + "/Compare/Ajax_Update",
        type: "post",
        dataType: "json",
        data: JSON.stringify(fvm)
    });
于 2013-06-06T05:20:33.537 回答
0

只是基于我过去做过的类似事情,我会像这样构建你的代码:

// if your C# is
public void Ajax_Update(CompareFVM fvm) {
}

// then your ajax call should be along the lines of
$.ajax({
    data : {
        'data' : [
            { /* compareItem */ },
            { /* compareItem */ },
            // ...
        ]         
    }
})

事情是,你的 C# 端点需要一个对象,所以你应该给它一个 JSON 对象。

如果您的 C# 类是

public class CompareFVM {
    public IList<CompareItem> data { get;set; }
}

那么你对应的 JSON 应该是:

{ 'data' : [] }

where.data将是一个CompareItem对象数组。

例如

{ 
    'data' : [
        {'prodId':'3175','stageId':'19045','value':'TUE','property':'despatchDay'},
        {'prodId':'3175','stageId':'19045','value':'TUE','property':'despatchDay'}
    ]
}
于 2013-06-06T05:24:46.143 回答