Skip to content

3. Data Binding

Emrah KONDUR edited this page Dec 17, 2022 · 11 revisions

URL

Set the URL to retrieve data from any action with GET or POST method:

.URL(Url.Action("GetAll", "Person"), "POST")

If property names are pascal case like "FirstName" and JSON naming policy is camel case like "firstName", enable it.

.URL(Url.Action("GetDataResult"), "POST", camelCase: true)

Use DataRequest object to bind parameters of request. With .ToDataResult(request); extension method, if there is entity framework, execute IQueryable list server side from context. Otherwise manipulate data manually and return DataResult<T> object including data.

using DatatableJS.Data;

public JsonResult GetAll(DataRequest request)
{
     DataResult<Person> result = ctx.People.ToDataResult(request);
     return Json(result);
}

ServerSide

Enable server-side processing mode. By default DataTables operates in client-side processing mode. Reference:

.ServerSide(true)

Data

Pass additional parameters with .Data("addParam") method and add script function to view.

function addParam() {
   return { Param1: "test1", Param2: true, Param3: 5 };
}

Get additional parameters with names.

public JsonResult GetAll(DataRequest request, string Param1, bool Param2, int Param3)
{
     //
}

Or get parameters with object reference named "data".

public JsonResult GetAll(DataRequest request, AddData data)
{
     //
}

Filters

Add additional filters before render datatable object. These filters are included in .ToDataResult(request) method with request while server side executing.

.Filters(filter =>
{
    filter.Add(a => a.BirthDate).LessThanOrEqual(DateTime.Now);
    filter.Add(a => a.Age).NotEqual(25);
})

Orders

Using this method you can define which column(s) the order is performed upon, and the ordering direction. Reference

.Orders(order => {
    order.Add(p => p.Name, OrderBy.Descending);
    order.Add(p => p.Age, OrderBy.Ascending);
})

Length Menu

This method allows you to readily specify the entries in the length drop down select list that DataTables shows . Reference

.LengthMenu(new int[] {5,10,15})

Set hasAll paramter true to show All option in select.

.LengthMenu(new int[] {5,10,15}, true)

Set allText paramter to change name of option All.

.LengthMenu(new int[] {5,10,15}, true, "All Pages")

Page Length

Number of rows to display on a single page when using pagination. Reference

.PageLength(15)
Clone this wiki locally