Я следую этому руководству, чтобы узнать и понять о jquery datatables.
Но я не получаю никаких данных в браузере. Пробовал 3 раза, прежде чем опубликовать этот вопрос в stackoverflow.
Может кто-нибудь сказать, где я делаю не так?
Ниже мой Index.cshtml:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_DataTablesLayout.cshtml";
}
<h2>Index</h2>
<table id="customersTable">
<thead>
<tr>
<th>Customer ID</th>
<th>Company Name</th>
<th>Contact Name</th>
<th>Contact Title</th>
<th>Address</th>
</tr>
</thead>
</table>
@section scripts {
}
<script>
$(function () {
$("#customersTable").DataTable({
"ajax": {
"url": "DataTables/GetList",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "CustomerID" },
{ "data": "CompanyName" },
{ "data": "ContactName" },
{ "data": "ContactTitle" },
{ "data": "Address" }
]
});
});
</script>
Ниже приведена страница макета: _DataTablesLayout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="~/Content/DataTables/css/jquery.dataTables.css" rel="stylesheet" />
<script src="~/scripts/jquery-3.3.1.js"></script>
<script src="~/scripts/DataTables/jquery.dataTables.js"></script>
</head>
<body>
<div>
@RenderBody()
@RenderSection("scripts", required: false)
</div>
</body>
</html>
Ниже приведен код в DataTablesController
public class DataTablesController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetList()
{
NorthwindEntities db = new NorthwindEntities();
var customersList = db.Customers.Select(x => new {
x.CustomerID,
x.CompanyName,
x.ContactName,
x.ContactTitle,
x.Address
}).ToList();
return Json(new { data = customersList }, JsonRequestBehavior.AllowGet);
}
}
У меня просто пустой экран. Я также не получил ошибок JavaScrip при проверке с помощью инструментов разработчика F12 в Internet Explorer.