【问题标题】:JQuery datatable server-side columns collectionJQuery 数据表服务器端列集合
【发布时间】:2014-09-06 19:26:21
【问题描述】:

我正在使用 Asp.Net MVC,我所需要的只是获取服务器上的列集合,而无需创建自定义模型绑定器。

如果我们查看Datatable docs,我们将看到columns[i].name 参数。

所以我创建了 model:

public class DtSentParameters
{
    public int Draw { get; set; }
    public int Start { get; set; }
    public int Length { get; set; }

    public IList<DtColumn> Columns { get; set; } //We are talking about this collection
}

在哪里DtColumn

public class DtColumn
{
    public string Name { get; set; }
    public string Data { get; set; }
}

控制器

public JsonResult GetSales(DtSentParameters data)
{
    //here placed some logic, but it's not important for us.
    return Json(data);
}

数据表 JS

dataTable({
    serverSide: true,
    ajax: {
        "url": url,
        "type": "POST"
    },
    processing: true,
    columns: [
        { data: "Date", name: "Date" },
        { data: "ClientAlias", name: "ClientAlias" }
    ],
    scrollCollapse: true,
    paging: true,
    jQueryUI: false
});

一切正常,我得到了我的数据,我可以使用分页。所以我正在尝试添加订购,这是一个问题。我的Columns.Count 等于 2 但名称和数据为空。

我检查了Request.Form.AllKeys

我们所拥有的,参数columns[i].name 看起来像columns[i][name]。顺便说一句,Request.Form["columns[1][name]"] 返回 ClientAlias,所以它的值正确但名称错误。

所以也许有人知道为什么参数名称与官方文档不同,我该如何更改它。

我只看到我的问题的一种设计

  • 自定义模型绑定器。

但我想要的只是将数据表用作docs says

任何想法如何在服务器上使用columns[i].name 而不是columns[i][name]

【问题讨论】:

    标签: c# asp.net-mvc-4 jquery-datatables


    【解决方案1】:

    我通过使用未记录或良好的隐藏功能解决了我的问题。至少我没找到HERE

    我所做的只是改变了我的 dataTable 初始化脚本

    dataTable({
      serverSide: true,
      ajax: {
          url: url,
          type: "POST",
          contentType: 'application/json; charset=utf-8', //set contentType,
          data: function(d) {
                return JSON.stringify(d);}      //stringify object by myself
           }
       });
    

    它还活着。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 2021-01-30
      • 2014-04-01
      相关资源
      最近更新 更多