Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET MVC Datatables响应有效,表不';不渲染_C#_Javascript_Jquery_Asp.net Mvc_Datatables - Fatal编程技术网

C# .NET MVC Datatables响应有效,表不';不渲染

C# .NET MVC Datatables响应有效,表不';不渲染,c#,javascript,jquery,asp.net-mvc,datatables,C#,Javascript,Jquery,Asp.net Mvc,Datatables,我有一个带有操作的服务控制器: [WebMethod] [ScriptMethod] public JsonResult GetDrugClasses() { var dataAccess = new DataAccessLayer(); var drugClasses = new List<DrugClassTable>(); drugClasses = dataAccess.GetDrugClasses

我有一个带有操作的服务控制器:

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

 public IEnumerable<DrugClassTable> DrugClasses { get; set; }
    public IEnumerable<SideEffects> SideEffects { get; set; }
    public IEnumerable<DrugColor> DrugColors { get; set; }
药品分类表

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

在GetDrugClasses中,您正在拉取的数据集有多大?您的方法可能会返回一个不完整/格式错误的值(可能是null),而datatables不需要它


第一步是修改您的控制器,并说,对GetDrugClasses执行Take(10)或Take(1)(如果值格式错误,让我们看看前几个项是否有效)。如果仍然失败,请尝试用手动设置的字符串值模拟一个假的DrugClass对象。如果这不起作用,可以尝试dataTables库的旧/新版本(以防dataTables出现错误)

您将表设置为期望
DrugColor
,但您发送了
DrugClassTable

我假设您打算为
DrugClassTable
设置您的表。改变它,看看它是否有效

比如说,

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

这是一个如何设置的示例。

它相对较小(140条记录),但取10条记录并没有得到不同的结果。您能给我们看一下您的模型吗?粘贴控制器返回的JSON-前几行。ID在哪里?应该有一个DrugColorId。@MiniRagnarok表的id在HTML中,myTable@wootscootinboogie很抱歉我在问有关DrugColorId的问题。如果我正在使用我编写的服务,如何在返回的JSON中添加像“aaData”这样的属性?@wootscootinboogie你可以看看Darin是如何做到的。
public class DrugColor
    {
        public int DrugColorId { get; set; }
        public string DrugColorDescription { get; set; }
        public string DrugClass { get; set; }
    }
public class DrugClassTable
    {
        public string DrugClass { get; set; }
        public string DrugDescription { get; set; }
    }
<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>
{
  "aaData": [
    [
       {
          "DrugClass":"A1A",
          "DrugDescription":"DIGITALIS GLYCOSIDES                               "
       },
       {
          "DrugClass":"A1B",
          "DrugDescription":"XANTHINES                                                   "
       }
    ]
  ]
}