title: Library For JQuery DataTables ASP.NET MVC
body: <p>i want to use jQuery DataTables in my ASP.NET MVC 5 Project.</p>
  
  <p>I did actually used the datatable and its working perfectly fine, but problem is i had to manually set the filter and queries, and i have a feeling the way i am trying to implement the datatables in mvc is not quite right.
  Yes i do get the results but i want to follow and standards plus i want something that i do not have to type the filter and pagination code again and again, i only send parameters to some function or class and i get the result.</p>
  
  <p>This is why i am trying to look for some datatables library with proper documentation.</p>
  
  <p>As i came across this library.</p>
  
  <pre><code>https://github.com/ALMMa/datatables.mvc
  </code></pre>
  
  <p>but there is no good documentation that i could understand what really is happening in that library or how to use that library?</p>
  
  <p>Yes i tried that library but due to lack of knowledge of c# and asp.net i don't understand how to implement it and i cant find any example related to this library to which i could understand the working of this library..</p>
  
  <p>however i also stumbled across this good documented process.</p>
  
  <pre><code>http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
  </code></pre>
  
  <p>It did worked great as they provided a very good detailed explanation of how to implement.</p>
  
  <p>I made my Controller Code Something like this.</p>
  
  <pre><code>public ActionResult Index(jQueryDataTableParamModel param = null)
          {
              if (Request.IsAjaxRequest() &amp;&amp; param != null)
              {
  
                  var allCategories = _db.Categories.ToList();
                  IEnumerable&lt;Category&gt; categories;
                  var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
                  var sortDirection = Request["sSortDir_0"]; // asc or desc
                  Func&lt;Category,string&gt; orderingFunction = (c =&gt; sortColumnIndex==1? c.Name :
                      sortColumnIndex==2? c.SortOrder.ToString(): c.Status.ToString());
  
                  if (!string.IsNullOrEmpty(param.sSearch))
                  {
                      if(sortDirection == "desc"){
                      categories = (from category in allCategories
                                    where category.Name.ToLower().Contains(param.sSearch.ToLower())
                                    select category).OrderByDescending(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                          }
                      else
                      {
                          categories = (from category in allCategories
                                        where category.Name.ToLower().Contains(param.sSearch.ToLower())
                                        select category).OrderBy(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                      }
                  }
                  else
                  {
                      if (sortDirection == "desc") { 
                      categories = (from category in allCategories
                                    select category).OrderByDescending(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                      }
                      else{
                          categories = (from category in allCategories
                                        select category).OrderBy(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                      }
                  }
                  var actionButtons = "&lt;div class='btn-group'&gt;"+
                      "&lt;button class='btn btn-primary btn-gradient btn-sm' type='button'&gt;"+
                      "&lt;span class='fa fa-pencil'&gt;&lt;/span&gt;"+
                      "&lt;/button&gt;";
                  return Json(new
                  {
                      sEcho = param.sEcho,
                      iTotalRecords = categories.Count(),
                      iTotalDisplayRecords = categories.Count(),
                      aaData = (from category in categories
                                select new[] { category.CategoryID.ToString(), category.Name, category.SortOrder.ToString(), actionButtons }).ToArray()
                  },
                                  JsonRequestBehavior.AllowGet);
              }
  
              return View();
          }
  </code></pre>
  
  <p>But as you see there is alot of code in just 1 method now if there are more methods for the datatable, i will have to write all the code again and again. so instead is it possible i make some kind of common datatables class or function and call it by providing some parameters and i get the desired result.</p>
  
  <p>I have no experience in ASP.NET MVC5 or C# ,its been some days i am working on MVC 5. So if there are any better ways of achiving the results what i have implemented here in my code please share that too and any suggestion you think you think should be nice for me regarding the datatables.</p>