Datatables server-side paging, sorting and filtering in angularjs

In the previous article, I have explained how to Implement datatables in AngularJS and asp.net mvc. Today we will see datatables server-side paging, sorting and filtering in angularjs and asp.net mvc.
Datatables in AngularJS and asp.net MVC
Datatables server-side paging, sorting and filtering in angularjs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Dynamic;

namespace MvcNGDatatable.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult getData()
        {
            //Datatable parameter
            var draw = Request.Form.GetValues("draw").FirstOrDefault();
            //paging parameter
            var start = Request.Form.GetValues("start").FirstOrDefault();
            var length = Request.Form.GetValues("length").FirstOrDefault();
            //sorting parameter
            var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
            var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
            //filter parameter
            var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();
            List allCustomer = new List();
            int pageSize = length != null ? Convert.ToInt32(length) : 0;
            int skip = start != null ? Convert.ToInt32(start) : 0;
            int recordsTotal = 0;
            //Database query
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                var v = (from a in dc.Customers select a);
                //search
                if (!string.IsNullOrEmpty(searchValue))
                {
                    v = v.Where(a => 
                        a.CustomerID.Contains(searchValue) ||
                        a.CompanyName.Contains(searchValue) ||
                        a.ContactName.Contains(searchValue) || 
                        a.Phone.Contains(searchValue) ||
                        a.City.Contains(searchValue)
                        );
                }

                //sort
                if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
                {
                    //for make sort simpler we will add Syste.Linq.Dynamic reference
                    v = v.OrderBy(sortColumn + " " + sortColumnDir);
                }

                recordsTotal = v.Count();
                allCustomer = v.Skip(skip).Take(pageSize).ToList();
            }

            return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = allCustomer });
        }
	}
}
Posted By :