Existing answers may refer to the old version of dataTable, but current versions (I use 1.10+) pass the initial record and length, so any sentence pageNo * pageSizewill give incorrect results.
The first simple “manual” approach
, , , Http Request start length. search[value] order[0][column] order[0][dir] ..
, , :
, HTTP-:
int startRec = 0;
int.TryParse(Request["start"], out startRec);
int pageSize = 10;
int.TryParse(Request["length"], out pageSize);
var search = Request["search[value]"];
var order = Request["order[0][column]"];
var direction = Request["order[0][dir]"];
var query = this._dataStore.Records.AsQueryable();
( ) :
if (!string.IsNullOrWhiteSpace(search))
{
query = query.Where(x => x.Label.ToLower().Contains(search.ToLower()));
}
:
switch (order)
{
case "0":
query = (direction == "desc") ? query.OrderByDescending(x => x.Id) : query.OrderBy(x => x.Id);
break;
case "1":
query = (direction == "desc") ? query.OrderByDescending(x => x.Label) : query.OrderBy(x => x.Label);
break;
}
, :
query = query.Skip(startRec).Take(pageSize);
.
( "Datatables.net MVC5" )
, /utils . MVC 5 Datatables.net MVC5 nuget.
.
public JsonResult Table([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestmodel)
- :
.
if (!string.IsNullOrEmpty(requestmodel.Search.Value))
{
query = query.Where(x => x.CompanyTypeName.Contains(requestmodel.Search.Value) || x.CompanyTypeDescription.Contains(requestmodel.Search.Value));
}
- :
.
foreach (var sort in requestmodel.Columns.GetSortedColumns())
{
switch (sort.Name)
{
case "CompanyTypeDescription":
query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeDescription) : query.OrderByDescending(x => x.CompanyTypeDescription);
break;
case "CompanyTypeName":
default:
query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeName) : query.OrderByDescending(x => x.CompanyTypeName);
break;
}
}
- ,
Skip Take, :
.
var result = query.Skip(requestmodel.Start).Take(requestmodel.Length).Select(x => new { x.CompanyTypeName, x.CompanyTypeDescription });
- , , JSON
DataTablesResponse:
.
return Json(new DataTablesResponse(requestmodel.Draw, result, query.Count(), base.RefSureContext.CompanyType.Count()), JsonRequestBehavior.AllowGet);
, , .
.