【问题标题】:jQuery Datatables with MVC Issue带有 MVC 问题的 jQuery 数据表
【发布时间】:2017-10-04 04:15:58
【问题描述】:

在将 Datatables 与 MVC 一起使用时,我收到错误 Requested unknown parameter '0' for row 0, column 0

我的用例是我想从 MVC 控制器返回 JSON 数据,但我可以将所有排序功能分页留在客户端上,因为我只会返回大约 100 行

我的代码如下:

public ActionResult LoadCarData()
{
  // will really come from DB just mocked for now
  var carData = new List<CarData>(new[]
  {
            new CarData { DT_RowId = "row_1", // will really have a CarId in the DB and I want to concatenate that to make each row have the id from the db
                            Manufacturer = "BMW",
                            Model = "3 Series",
                            Colour = "Red",
                            EngineSize = 3.0M,
                          },
            new CarData { DT_RowId = "row_2",
                            Manufacturer = "Mercedes",
                            Model = "C Class",
                            Colour = "White",
                            EngineSize = 2.5M,
                          },
            new CarData { DT_RowId = "row_3",
                            Manufacturer = "Audi",
                            Model = "A5",
                            Colour = "Black",
                            EngineSize = 2.0M,
                          }
        });

  return Json(new
  {
    aaData = carData
  }, JsonRequestBehavior.AllowGet);
}

我的 CarData 类如下:

  public class CarData
  {
    public string DT_RowId { get; set; } 
    public string Manufacturer { get; set; }
    public string Model { get; set; }
    public string Colour { get; set; }
    public decimal? EngineSize { get; set; }
  }

我有一个对我的 MVC 控制器的 ajax 调用,如下所示(注意 DataTable 中的第一列是一个复选框,允许用户选择该行:

  $.ajax({
        url: '@Url.Action("LoadCarData", "Car")',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
          console.log(data);
          $('#carData').dataTable({
            bProcessing: true,
            aaData: data.aaData,
            columnDefs: [
              { orderable: false, className: 'select-checkbox', targets: 0 },
            ],
            select: {
              style: 'multi',
              selector: 'td:first-child'
            },
            order: [[1, 'asc']]
          });
        }
      });

我的表格 HTML 如下:

<table id="carData" class="display" cellspacing="0" style="width:100%">
  <thead>
    <tr>
      <th></th>
      <th>Manufacturer</th>
      <th>Model</th>
      <th>Colour</th>
      <th>EngineSize</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

最后,在 Chrome 的 DevTools 的 Network 选项卡中,我可以看到返回的 JSON 数据如下:

{
    "aaData": [{
        "DT_RowId": "row_1",
        "Manufacturer": "BMW",
        "Model" = "3 Series",
        "Colour": "Red",
        "EngineSize": 3.0,
    }, {
        "DT_RowId": "row_2",
        "Manufacturer": "Mercedes",
        "Model" = "C Class",
        "Colour": "White",
        "EngineSize": 2.5,
    }, {
        "DT_RowId": "row_3",
        "Manufacturer": "Audi",
        "Model" = "A5",
        "Colour": "Black",
        "EngineSize": 2.0,
    }]
}

为了正确输出每一行呈现为来自数据库的行的 rowId,我做错了什么吗?

【问题讨论】:

    标签: jquery ajax asp.net-mvc datatable datatables


    【解决方案1】:

    我是初学者,只是想提供帮助。看看你是否至少得到表格数据。可以试试下面的sn-p吗:

      $.ajax({
        url: '@Url.Action("LoadCarData", "Car")',
        type: 'GET',
        dataType: 'json',
        success: function (data) {
          console.log(data);
          $('#carData').dataTable({
            columns: [
              { data: "data.aaData" }
            ]
          });
        }
      });
    

    或者类似下面的东西

    尝试用以下替换数据

    attempt 1: data: "data.aaData.Model"
    attempt 2: data : "aaData.Model"
    attempt 3: data : "aaData"
    

    【讨论】:

    • 您好,感谢您的回复。我稍后会尝试并通知您
    • 在第二个 sn-p 中,.Model 是否来自 aaData?
    • 你可能是对的。由于您嵌套了 JSON 结构,而我试图从 JSON 中获取一列数据。不管怎样,试试看结果如何。
    • 好的..会告诉你的
    • 是的,它可以工作了...不幸的是,您上面的解决方案对我不起作用..稍后再发布我的解决方案
    猜你喜欢
    • 2018-05-21
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多