JQuery数据表无法在ASP.NET MVC 5上工作

s5a0g9ez  于 2022-12-01  发布在  .NET
关注(0)|答案(1)|浏览(147)

我已经使用nuget Package Manager控制台和命令install-package jquerydatatablesmvc将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>

我哪里搞砸了?

i7uq4tfw

i7uq4tfw1#

这里我已经讲解了here如何在asp.net MVC应用程序中一步一步地实现jQuery DataTable。
只需按照几个步骤在应用程序中完成该工作
第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);
  }
}

步骤2:编写html和jQuery以显示数据库数据
完整的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>
}

相关问题