i want to use jQuery DataTables in my ASP.NET MVC 5 Project.
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.
This is why i am trying to look for some datatables library with proper documentation.
As i came across this library.
https://github.com/ALMMa/datatables.mvc
but there is no good documentation that i could understand what really is happening in that library or how to use that library?
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..
however i also stumbled across this good documented process.
http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
It did worked great as they provided a very good detailed explanation of how to implement.
I made my Controller Code Something like this.
public ActionResult Index(jQueryDataTableParamModel param = null)
{
if (Request.IsAjaxRequest() && param != null)
{
var allCategories = _db.Categories.ToList();
IEnumerable<Category> categories;
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
var sortDirection = Request["sSortDir_0"]; // asc or desc
Func<Category,string> orderingFunction = (c => 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 = "<div class='btn-group'>"+
"<button class='btn btn-primary btn-gradient btn-sm' type='button'>"+
"<span class='fa fa-pencil'></span>"+
"</button>";
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();
}
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.
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.