2016-10-29 96 views
0

下面是我的cshtml视图文件中的完整代码。为什么JQuery.Datatable没有渲染,也没有给出错误?

我得到一个很好的网页格式化表。

但它没有被JQuery.Datatable格式化。我可以删除所有的脚本行,它看起来仍然是一样的。

我可能会做错什么。我没有得到任何与JQuery相关的构建错误。

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Employee Index Page</title> 

    <script type="text/javascript" language="javascript" src="~/Scripts/jquery-1.10.2.js"> 
    </script> 
    <script type="text/javascript" language="javascript" src="~/Scripts/DataTables/jquery.dataTables.js"> 
    </script> 

    <script > 
     $(document).ready(function() { 
     $('#example').DataTable(); 
       }); 
    </script> 
</head> 
<body> 
    <div> 
     <h1>Employee Index Page</h1> 
     <table id="example" class="display" cellspacing="0" width="100%"> 
      <thead> 
       <tr> 
        <th>Symbol</th> 
        <th>ActualPosition</th> 
        <th>AccountIdent</th> 
       </tr> 
      </thead> 
      <tbody> 

       @{ 
        var categories = (DataTable)ViewData["MyData"]; 

        foreach (DataRow item in categories.Rows) 
        { 
         string symbol = item.Field<string>("Symbol"); 
         string actualPositionText = item.Field<long>("ActualPosition").ToString(); 
         string accountIdent = item.Field<string>("AccountIdent"); 

         <tr> 
          <td>@symbol </td> 
          <td>@actualPositionText</td> 
          <td>@accountIdent </td> 
         </tr> 
        } 
       } 
      <tbody> 
     </table> 


    </div> 

</body> 
</html> 
+0

你确定浏览器正在拉下dataTable脚本吗?您在调试器控制台中看到任何错误吗? – Kye

+0

完全没有错误 – ManInMoon

+0

你能否确认数据表和jQuery都已加载? – Terry

回答

-1

视图错过了CSS文件

<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Employee Index Page</title> 
    <link href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css" /> 
</head> 
+0

没有帮助 – ManInMoon

+0

将渲染代码放在主体的最后部分不在头部

+0

再次,它没有任何区别。我真的被卡住了 – ManInMoon

0

我试图重现这一点,但我一直没能如愿。请致电this fiddle。它包含与你的示例相同的代码,唯一的区别是jquery和datatable脚本已经从CDN引用,并且它的工作原理!

所以,我想你可能有一些与你服务的本地文件或类似的问题。

希望这会有所帮助!

0

尝试将dataTables示例html添加到您的页面以排除任何mvc或HTML语法错误。