I've used the nuget Package Manager Console with the command install-package jquerydatatablesmvc
to install jquery datatables to my MVC 5 project and it installed version 1.9.4.
我已经使用nuget Package Manager Console和命令install-package jquerydatatablesmvc将jquery数据表安装到我的MVC 5项目并安装了1.9.4版本。
However, after including the required scripts and css files, the data table is not still working.
但是,在包含所需的脚本和css文件之后,数据表仍然无法正常工作。
Here is what I've added to the page:
这是我添加到页面中的内容:
<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>
And the jquery code:
和jquery代码:
<script type="text/javascript">
$(document).ready(function () {
$('#myTable').DataTable();
});
</script>
Here is the actual table:
这是实际的表格:
<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>
Where did I mess up?
我在哪里陷入困境?
1 个解决方案
#1
0
Here I have explained here how to implement jQuery DataTable in asp.net MVC application step by step.
这里我解释了如何在asp.net MVC应用程序中逐步实现jQuery DataTable。
just follow a few steps for doing that work in your application
只需按照几个步骤在您的应用程序中完成该工作
Step 1 : Write a MVC action for fetch data from database
步骤1:为从数据库中获取数据编写MVC操作
public ActionResult loaddata()
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
var data = dc.Customers.OrderBy(a => a.ContactName).ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
}
Step 2 : Write html and jQuery for show database data
第2步:为show database数据编写html和jQuery
Complete HTML code
完整的HTML代码
@{
ViewBag.Title = "Index";
}
<h2>Part 1 : Implement jQuery Datatable in ASP.NET MVC</h2>
<div style="width:90%; margin:0 auto;">
<table id="myTable">
<thead>
<tr>
<th>Employee Name</th>
<th>Company</th>
<th>Phone</th>
<th>Country</th>
<th>City</th>
<th>Postal Code</th>
</tr>
</thead>
</table>
</div>
<style>
tr.even {
background-color: #F5F5F5 !important;
}
</style>
@* Load datatable css *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
"url": "/home/loaddata",
"type": "GET",
"datatype": "json"
},
"columns" : [
{ "data": "ContactName", "autoWidth": true },
{ "data": "CompanyName", "autoWidth": true },
{ "data": "Phone", "autoWidth": true },
{ "data": "Country", "autoWidth": true },
{ "data": "City", "autoWidth": true },
{ "data": "PostalCode", "autoWidth": true }
]
});
});
</script>
}
#1
0
Here I have explained here how to implement jQuery DataTable in asp.net MVC application step by step.
这里我解释了如何在asp.net MVC应用程序中逐步实现jQuery DataTable。
just follow a few steps for doing that work in your application
只需按照几个步骤在您的应用程序中完成该工作
Step 1 : Write a MVC action for fetch data from database
步骤1:为从数据库中获取数据编写MVC操作
public ActionResult loaddata()
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
var data = dc.Customers.OrderBy(a => a.ContactName).ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
}
Step 2 : Write html and jQuery for show database data
第2步:为show database数据编写html和jQuery
Complete HTML code
完整的HTML代码
@{
ViewBag.Title = "Index";
}
<h2>Part 1 : Implement jQuery Datatable in ASP.NET MVC</h2>
<div style="width:90%; margin:0 auto;">
<table id="myTable">
<thead>
<tr>
<th>Employee Name</th>
<th>Company</th>
<th>Phone</th>
<th>Country</th>
<th>City</th>
<th>Postal Code</th>
</tr>
</thead>
</table>
</div>
<style>
tr.even {
background-color: #F5F5F5 !important;
}
</style>
@* Load datatable css *@
<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"ajax": {
"url": "/home/loaddata",
"type": "GET",
"datatype": "json"
},
"columns" : [
{ "data": "ContactName", "autoWidth": true },
{ "data": "CompanyName", "autoWidth": true },
{ "data": "Phone", "autoWidth": true },
{ "data": "Country", "autoWidth": true },
{ "data": "City", "autoWidth": true },
{ "data": "PostalCode", "autoWidth": true }
]
});
});
</script>
}