c# - 将数据表 POST 数组转换为 C# 模型

标签 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

我如何选择/绑定(bind)此数据到 C# 类,它是 Action 方法的参数,实际开始、长度、绘制 map 但是 列[n][nnn] 给空

我的 C# 模型类是 公共(public)类 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; }
}

谢谢

最佳答案

不,这不是我使用的最新版本不发布 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' },
        ]
    });

关于c# - 将数据表 POST 数组转换为 C# 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578663/

相关文章:

c# - Caliburn Micro 和 ModernUI 示例/教程

c# - JavaScript 未在 .NET WebBrowser 控件中执行

JQuery Datatables服务器端处理表不刷新内容

c# - 如何将 namevaluecollection 自动映射到强类型类?

c# - 为什么我的目录搜索需要这么长时间?

c# - 通过引用传递 : Which is more readable/right?

javascript - jQuery、对话框、选项卡和数据表 : how to wait for one to finish before going ahead?

jQuery DataTables 过滤多个表

html - DataTable JQuery 导出 PDF 显示错误方向的阿拉伯数据

jquery - 数据表中的标题高度