【发布时间】:2015-08-15 01:06:19
【问题描述】:
我使用带有命令install-package jquerydatatablesmvc 的 nuget 包管理器控制台将 jquery 数据表安装到我的 MVC 5 项目并安装了 1.9.4 版。
但是,在包含所需的脚本和 css 文件后,数据表仍然无法正常工作。
这是我添加到页面的内容:
<link href="~/Content/DataTables-1.9.4/media/css/jquery.dataTables.css" rel="stylesheet" />
<script src="~/Scripts/DataTables-1.9.4/media/js/jquery.js"></script>
<script src="~/Scripts/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
还有jquery代码:
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable();
});
</script>
这是实际的表格:
<table class="table" id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Registered By</th>
<th>Is Active</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => m.Name)</td>
<td>@Html.DisplayFor(modelItem => m.RegisteredBy)</td>
<td>@Html.DisplayFor(modelItem => m.IsActive)</td>
<td>@Html.ActionLink("Edit", "Edit", new { id = m.Id }) |
@Html.ActionLink("Details", "Details", new { id = m.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = m.Id })</td>
</tr>
}
</tbody>
</table>
我在哪里搞砸了?
【问题讨论】:
-
浏览器的控制台有错误吗?
-
哦,我没有检查。现在,它说“Uncaught TypeError: $(...).DataTable is not a function”
-
您还有其他关于 jQuery 的参考资料吗? (我在您的代码 sn-p 中看到了一个,但页面中是否还有其他引用它?)如果您有多个对 jQuery 的引用,这可能是导致它的原因
-
主布局页面上有这一行:@Scripts.Render("~/bundles/jquery")
-
最后一个错误意味着 jQuery 现在没有初始化。您能否验证您的 ~/bundles/jQuery 捆绑包是否引用了 jQuery,并且它是否出现在数据表脚本标记上方?如果是这样,那么您可能需要发布页面的 html 源代码(由浏览器呈现) - 只是脚本标签和相关的数据表。
标签: jquery asp.net asp.net-mvc asp.net-mvc-5 jquery-datatables