I try to follow this tutorial on how to use DataTables plugin: https://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part?fid=1609174&df=90&mpp=25&sort=Position&spc=Relaxed&tid=4604201 but with using EF.
I've installed via package manager "Mvc.JQuery.DataTables".
This is my Controller (db is made out of generated edmx from Database, jQueryDataTableParamModel is a copy from a tutorial):
public class TablesController : Controller
{
public ActionResult Index()
{
return View();
}
// AjaxHandler
public ActionResult AjaxHandler(jQueryDataTableParamModel param)
{
var result = from p in db.Tables
select p;
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = db.Tables.Count(),
iTotalDisplayRecords = db.Tables.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
}
This is my model class:
public partial class Table
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Date { get; set; }
public Nullable<decimal> DateValue { get; set; }
public Nullable<int> cs_Table { get; set; }
public virtual Surname Surname1 { get; set; }
public virtual Date Date1 { get; set; }
public virtual Name Name1 { get; set; }
}
This is my View:
@model IEnumerable<EFModel.Table>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table" id="myDataTable">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.DateValue)
</th>
<th>
@Html.DisplayNameFor(model => model.cs_Table)
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
And this is my index.js with function for handling tables:
$(document).ready(function () {
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "Tables/AjaxHandler",
"bProcessing": true,
"aoColumns": [
{
"sName": "ID",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj) {
return '<a href=\"Details/' +
oObj.aData[0] + '\">View</a>';
}
},
{ "sName": "Name" },
{ "sName": "Surname" },
{ "sName": "Date" },
{ "sName": "DateValue" }
{ "sName": "cs_Tables"}
]
});
});
I import needed files for DataTables to work in the Head in _Layout.cshtml:
<script src="~/Scripts/jquery-3.3.1.min.js"
type="text/javascript"></script>
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"
type="text/javascript"></script>
<script src="~/Scripts/DataTables/index.js"
type="text/javascript"></script>
From what I've read it should suffice to display records in my view Index but nothing comes up. I've put breakpoint on AjaxHandler method but it doesn't go there.