【问题标题】:Datatables not displaying properly in MVC6数据表在 MVC6 中无法正确显示
【发布时间】:2016-01-29 19:23:14
【问题描述】:

我目前正在玩 MVC6 的第一个乐趣,但我在使用 Datatables 时遇到了一些问题。据我所知,我已经包含了应该存在的所有内容,但是每次我尝试运行代码时都会收到一个错误,提示 jquery.datatables.min 看不到 Jquery。

代码如下:

<link href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet" />
<script type="text/javascript" charset="utf-8" src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>

<script type="text/javascript" charset="utf-8">

$(document).ready( function () {

    $('#datatable2').DataTable();

} );

</script>

<table class="table table-striped table-hover" id="datatable2" cellspacing="0">
    <tr>
    <th>ID</th>
    <th>Problem Description</th>
    <th>Solution</th>
</tr>

@foreach(var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ProblemID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProblemDescription)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Solution)
        </td>
    </tr>
}

有什么想法吗?这可能是非常明显的事情......

*而且我知道脚本通常应该在 _layout 中,这只是一个测试项目,以感受 MVC 5 和 6 之间的变化。

【问题讨论】:

  • 尝试以下操作:将 jquery 作为您的第一个脚本,然后是数据表脚本。将&lt;body&gt;&lt;/body&gt; 添加到您的表中。查看文档:datatables.net/manual/installation
  • 两个优点 - 也是我应该想到的......现在我们得到:0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法'DataTable'
  • 你在 jquery.min.js 之前已经包含了 datatable.js 呃……:D 首先解决这个问题。
  • 另一件值得一提的事情是将你的脚本放在@section scripts { }

标签: jquery asp.net-mvc datatables


【解决方案1】:

尝试如下

如需了解更多信息,请关注此列表dataTables js Example

以这种方式包含文件

1) jquery.dataTables.min.css,

2) jquery-2.2.0.min.js,

3) jquery.dataTables.min.js

<link href="//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>

HTML

<table class="table table-striped table-hover" id="datatable2" cellspacing="0">
   <thead>
      <tr>
        <th>ID</th>
        <th>Problem Description</th>
        <th>Solution</th>
      </tr>
   </thead>
   <tbody>
        @foreach(var item in Model)
        {
           <tr>
               <td>
                   @Html.DisplayFor(modelItem => item.ProblemID)
               </td>
               <td>
                   @Html.DisplayFor(modelItem => item.ProblemDescription)
               </td>
               <td>
                   @Html.DisplayFor(modelItem => item.Solution)
               </td>
          </tr>
        }
   </tbody>
</table>

JS

<script type="text/javascript" charset="utf-8">    
    $(document).ready( function () {    
        $('#datatable2').DataTable();    
    });    
</script>

【讨论】:

  • 您好,感谢您的回复。不幸的是,没有骰子 - 我仍然收到 0x800a01b6 - JavaScript 运行时错误:对象不支持属性或方法“DataTable”
  • 点击此链接可能有助于解决您的问题stackoverflow.com/a/15759205/2798643
  • 鉴于我尝试过的随机东西的数量,这似乎是一个不错的建议,但我经历并删除了所有内容,但仍然没有好处
猜你喜欢
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多