简体   繁体   English

.NET MVC Datatables响应有效,表未呈现

[英].NET MVC Datatables response works, table doesn't render

I have a Services controller with an action: 我有一个操作的服务控制器:

   [WebMethod]
    [ScriptMethod]
    public JsonResult GetDrugClasses()
    {
        var dataAccess = new DataAccessLayer();
        var drugClasses = new List<DrugClassTable>();
        drugClasses = dataAccess.GetDrugClasses();
        return Json(drugClasses, JsonRequestBehavior.AllowGet);
    }

And in my view I have: 在我看来,我有:

<table id="myTable">
    <thead>
        <tr>
            <th>DrugColorID</th>
            <th>DrugColorDescription</th>
            <th>DrugClass</th>
        </tr>
    </thead>
</table>
<script type="text/javascript">
    $('#myTable').dataTable({
        'bServerSide': true,
        'sAjaxSource': '/Services/GetDrugClasses',
        'aoColumns': [
            { 'sName': 'DrugColorId' },
            { 'sName': 'DrugColorDescription' },
            { 'sName': 'DrugClass' }
            ]
    });
</script>

When I open up dev tools I can see the the request was successful and the correct response was received. 当我打开开发工具时,我可以看到请求成功并且收到了正确的响应。 When I use the above code I get a JavaScript error: Cannot read property length of undefined on line 2038 of the data tables version hosted here: http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.js 当我使用上面的代码时,出现JavaScript错误: Cannot read property length of undefined托管在此处的数据表版本的第2038行Cannot read property length of undefinedhttp : //ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery .dataTables.js

I have a Model with three lists which I will need to use client-side paging on. 我有一个包含三个列表的模型,需要使用客户端分页。 Datatables renders the search box, columns and paging controls correctly, just not the data. 数据表正确呈现搜索框,列和分页控件,而不是数据。 What am I missing here? 我在这里想念什么?

sample JSON: JSON示例:

 [
   {
      "DrugClass":"A1A",
      "DrugDescription":"DIGITALIS GLYCOSIDES                                        "
   },
   {
      "DrugClass":"A1B",
      "DrugDescription":"XANTHINES                                                   "
   },
   {
      "DrugClass":"A1C",
      "DrugDescription":"INOTROPIC DRUGS                                             "
   },
   {
      "DrugClass":"A1D",
      "DrugDescription":"GENERAL BRONCHODILATOR AGENTS                               "
   },
   {
      "DrugClass":"A1E",
      "DrugDescription":"XANTHINES/DIETARY SUPPLEMENT COMBINATIONS                   "
   },
   {
      "DrugClass":"A2A",
      "DrugDescription":"ANTIARRHYTHMICS                                             "
   },
   {
      "DrugClass":"A2B",
      "DrugDescription":"ANTIANGINAL, HEART RATE REDUCING, I(F) INHIBITOR            "
   },
   {
      "DrugClass":"A2C",
      "DrugDescription":"ANTIANGINAL & ANTI-ISCHEMIC AGENTS,NON-HEMODYNAMIC          "
   }
]

model: 模型:

 public IEnumerable<DrugClassTable> DrugClasses { get; set; }
    public IEnumerable<SideEffects> SideEffects { get; set; }
    public IEnumerable<DrugColor> DrugColors { get; set; }

DrugColor: 药物颜色:

public class DrugColor
    {
        public int DrugColorId { get; set; }
        public string DrugColorDescription { get; set; }
        public string DrugClass { get; set; }
    }

DrugClassTable 药物分类表

public class DrugClassTable
    {
        public string DrugClass { get; set; }
        public string DrugDescription { get; set; }
    }

How large is the dataset you are pulling down in GetDrugClasses? 您要在GetDrugClasses中提取的数据集有多大? It's possible your method returns an incomplete/malformatted value (null, perhaps?) where datatables isn't expecting it. 您的方法可能返回数据表不期望的不完整/格式错误的值(可能为空?)。

Step one is modify your controller and say, do Take(10) or Take(1) on GetDrugClasses (in case a value is malformatted, let's see if even the first few items work or not). 第一步是修改您的控制器,并说对GetDrugClasses进行Take(10)或Take(1)(以防万一值格式错误,让我们看看前几个项目是否起作用)。 If that still fails, try mocking out a fake DrugClass object with manually set string values. 如果仍然失败,请尝试使用手动设置的字符串值模拟伪造的DrugClass对象。 If that doesn't work, try maybe an older/newer version of the dataTables library (in case its a datatables bug). 如果这样不起作用,请尝试使用dataTables库的旧/新版本(以防出现datatables错误)。

You set up your table to expect DrugColor but you sent DrugClassTable . 您将表设置为使用DrugColor但发送了DrugClassTable

I assume that you meant to set your table up for DrugClassTable . 我假设您打算为DrugClassTable设置表。 Change that and see if it works. 更改它,看看是否可行。

For example, 例如,

<table id="myTable">
    <thead>
        <tr>
            <th>DrugClass </th>
            <th>DrugDescription </th>
        </tr>
    </thead>
</table>
<script type="text/javascript">
    $('#myTable').dataTable({
        'bServerSide': true,
        'sAjaxSource': '/Services/GetDrugClasses',
        'aoColumns': [
            { 'sName': 'DrugClass' },
            { 'sName': 'DrugDescription ' }
            ]
    });
</script>

As other people have mentioned, the json return is wrong. 正如其他人所提到的,json返回是错误的。 It should look like this: 它看起来应该像这样:

{
  "aaData": [
    [
       {
          "DrugClass":"A1A",
          "DrugDescription":"DIGITALIS GLYCOSIDES                               "
       },
       {
          "DrugClass":"A1B",
          "DrugDescription":"XANTHINES                                                   "
       }
    ]
  ]
}

Here's an example of how to set this up. 这是一个设置方法的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控件不会在asp.net mvc上呈现bootstrap fileupload - control doesn't render bootstrap fileupload on asp.net mvc jQuery DataTables不使用ASP.NET MVC 4 Razor显示数据 - jQuery DataTables doesn't display data with ASP.NET MVC 4 Razor asp .net核心应用程序没有在IE和EDGE中设置响应cookie,但在firefox和chrome中运行良好 - asp .net core app doesn't set response cookie in IE and EDGE but works well in firefox and chrome ASP.NET MVC 4-RouteConfig.cs-本地更新,可以工作-在服务器上不更新 - ASP.NET MVC 4 - RouteConfig.cs - updated locally, works - doesn't update on server AuthorizeAttribute适用于MVC Controller,但不适用于ApiController - AuthorizeAttribute works for MVC Controller but doesn't for ApiController 为什么dataTables不能与ASP.NET MVC 4 Razor一起使用 - Why won't dataTables work with ASP.NET MVC 4 Razor IIS 不呈现 ASP.net MVC 视图。 它列出了物理路径中的所有文件和文件夹 - IIS doesn't render the ASP.net MVC view. It lists down all files and folders inside the physical path WriteableBitmap 不能在 Linux 上呈现,但可以在 UWP 和 Wasm 上使用 - WriteableBitmap doesn't render on Linux but works on UWP and Wasm Style.Render 在服务器端不起作用 (Razor) - Style.Render doesn't works on server side (Razor ) 请求不等待.net中的响应 - Request doesn't wait for response in .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM