将Datatables POST数组转换为C#模型

Irf*_*raf 5 c# datatables

我正在使用datatables.net结构将数据发送到ASP MVC,如下所示

draw:1
columns[0][data]:first_name
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:last_name
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
order[0][column]:0
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false
Run Code Online (Sandbox Code Playgroud)

我如何选择/绑定此数据到c#类,它是Action方法的参数,实际上是开始,长度,绘制地图,但是column [n] [nnn]为null

我的c#模型类是公共类DataTableModel {

    public string draw { get; set; }
    public ICollection<Columns> columns { get; set; }
    //public string[] order { get; set; }
    public int start { get; set; }
    public int length { get; set; }
    public List<Order> order { get; set; }
    public int recordsTotal { get; set; }
    public int recordsFilter { get; set; }

    public DataTableModel()
    {
        columns = new List<Columns>();
        order = new List<Order>();
    }
}

public class Columns
{
    public string data { get; set; }
    public string name { get; set; }
    public string searchable { get; set; }
    public string orderable { get; set; }
    public List<Search> search { get; set; }
}

public class Search
{
    public bool regex { get; set; }
    public string value { get; set; }
}

public class Order
{
    public string column { get; set; }
    public string dir { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Irf*_*raf 3

不,情况并非如此,我使用的是不发布 iSortCol_n 的最新版本。答案是我没有包含'contentType': "application/json"。这就是为什么我在 mvc 操作参数中选择空列列表。一旦我放置它,mvc 就会映射所有字段。

 var table =   $('#example').dataTable({
        "processing": true,
        "serverSide": true,
        'ajax': {
            'type': 'POST',
            'contentType': "application/json",
            'url': '/TestDistributor/GetAllForGrid',
            'data': function (d) {
                console.log(JSON.stringify(d));
                return JSON.stringify(d);
            }
        },
        "columns": [
        { 'data': 'DistributorCode' },
        { 'data': 'DistributorName' },
        { 'data': 'AreaCode' },
        { 'data': 'TownCode' },
        { 'data': 'CityCode' },
        ]
    });
Run Code Online (Sandbox Code Playgroud)