[英].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 undefined
: http : //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 "
}
]
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.