Skip to content

Instantly share code, notes, and snippets.

@cynx
Created August 17, 2015 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cynx/fd01208f1a78a057dc09 to your computer and use it in GitHub Desktop.
Save cynx/fd01208f1a78a057dc09 to your computer and use it in GitHub Desktop.
HomeController.cs file for the JQUERY DATATABLES 1.10+ AND ASP.NET MVC 5 SERVER SIDE INTEGRATION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDatatableApp.Models;
namespace MVCDatatableApp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult DataHandler(DTParameters param)
{
try
{
var dtsource = new List<Customer>();
using (dataSetEntities dc = new dataSetEntities())
{
dtsource = dc.Customers.ToList();
}
List<String> columnSearch = new List<string>();
foreach (var col in param.Columns)
{
columnSearch.Add(col.Search.Value);
}
List<Customer> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
DTResult<Customer> result = new DTResult<Customer>
{
draw = param.Draw,
data = data,
recordsFiltered = count,
recordsTotal = count
};
return Json(result);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
}
}
@barts2108
Copy link

There is a minor issue with the recordsFiltered / recordsTotal counters

                draw = param.Draw,
                data = data,
                recordsFiltered = count,
                recordsTotal = count

If recrodsFiltered and recordsTotal are equal, the oLanguage: sInfoFilered text from the DataTable will never show up. I think that recordsTotal should be equal to dtsource.Count() so that you can show something line "Showing 1 to 10 from 128 entries (filtered from 35978 recrods) " so that in all cases you can see how many total records you have, how many match the filter.

                draw = param.Draw,
                data = data,
                recordsFiltered = count,
                recordsTotal = dtsource.Count()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment