I need some guidance on what to put in my controller so that I can use server-side processing with my jQuery datatables. I am using MVC 5 and Entity Framework.
The exam
Use EFDatatable
EFDatatable is a helper to create a grid with Jquery Datatable and provides an extension to retrive data generically from Entity Framework context. It possible to use many datatable.js features with Html helper. It gives serverside or client side options. There's more: Wiki Documentation
@(Html.EF().Datatable()
.Name("PersonGrid")
.Columns(cols =>
{
cols.Field(a => a.Id).Visible(false);
cols.Field(a => a.Name).Title("First Name").Class("text-danger");
cols.Field(a => a.Age).Title("Age").Searchable(false);
cols.Field(a => a.BirthDate).Title("Birth Date").Format("DD-MMM-Y");
cols.Command(a => a.Id, "onClick", text: "Click").Title("");
})
.Filters(filter =>
{
filter.Add(a => a.Id).GreaterThanOrEqual(1);
})
.URL(Url.Action("GetDataResult"), "POST")
.ServerSide(true)
.Render()
)
With "ToDataResult(request)" extension function, data can get with server side pagination very simply.
public JsonResult GetDataResult(DataRequest request)
{
DataResult result = context.People.ToDataResult(request);
return Json(result);
}
Install EFDatatable from the package manager console:
PM> Install-Package EFDatatable
Then add datatables.net Javascript and CSS files or links to your project.