Working With jQuery DataTables in MVC 4 - Part 3

This article explains how to use jQuery DataTables with in MVC 4.

If you are new to this plug-in then please go through the previous articles that will provide you the basics of the jQuery plug-in.

Let’s explore step-by-step how to use this plug-in within a MVC 4 application.

Step 1

Open Visual Studio and create the Empty MVC 4 application.

Step 2

Create a new Controller named “JQueryDataTableController” within the controller folder. 

Step 3

Within the Model folder just create a class named Employee.cs.

Step 4

Please find the following simple Employee class structure.

  1. class Employee  
  2. {  
  3.    public string ID { getset; }  
  4.    public string Name { getset; }  
  5.    public string Designation { getset; }  
  6.    public string Address { getset; }  
  7.    public string DateOfJoining { getset; }  
  8. }  

Step 5

Globally create a List<Employee> object to generate list of employees.

  1. List<Employee> objEmployee = new List<Employee>();  

Step 6

Within the JQueryDataTableController constructor method paste the following set of code. Before adding the Employee object into the List collection I am checking whether the count is 0, if it’s 0 then I am adding the entire employee object to the List collection.

  1. public JQueryDataTableController()   
  2. {  
  3.     if (objEmployee.Count == 0) {  
  4.         objEmployee.Add(new Employee {  
  5.             ID = "1", Name = "Sathish", Address = "Chennai", Designation = "Technical Lead", DateOfJoining = "10/01/2010"  
  6.         });  
  7.         objEmployee.Add(new Employee {  
  8.             ID = "2", Name = "Muthu", Address = "Chennai", Designation = "Technical Architect", DateOfJoining = "10/01/2011"  
  9.         });  
  10.         objEmployee.Add(new Employee {  
  11.             ID = "3", Name = "Arul", Address = "Delhi", Designation = "Technical Lead", DateOfJoining = "10/01/2015"  
  12.         });  
  13.         objEmployee.Add(new Employee {  
  14.             ID = "4", Name = "Avinash", Address = "Coimbatore", Designation = "Software Developer", DateOfJoining = "10/02/2012"  
  15.         });  
  16.         objEmployee.Add(new Employee {  
  17.             ID = "5", Name = "Bala", Address = "Madurai", Designation = "Technical Lead", DateOfJoining = "10/01/2015"  
  18.         });  
  19.         objEmployee.Add(new Employee {  
  20.             ID = "6", Name = "Selva", Address = "Delhi", Designation = "Project Manager", DateOfJoining = "10/01/2015"  
  21.         });  
  22.         objEmployee.Add(new Employee {  
  23.             ID = "7", Name = "Vaishanth", Address = "Chennai", Designation = "Technical Architect", DateOfJoining = "10/01/2012"  
  24.         });  
  25.         objEmployee.Add(new Employee {  
  26.             ID = "8", Name = "Prabhu", Address = "Chennai", Designation = "Technical Lead", DateOfJoining = "10/04/2015"  
  27.         });  
  28.         objEmployee.Add(new Employee {  
  29.             ID = "9", Name = "Rajesh", Address = "Delhi", Designation = "Software Developer", DateOfJoining = "10/01/2015"  
  30.         });  
  31.         objEmployee.Add(new Employee {  
  32.             ID = "10", Name = "Purushoth", Address = "Madurai", Designation = "Technical Lead", DateOfJoining = "10/06/2013"  
  33.         });  
  34.         objEmployee.Add(new Employee {  
  35.             ID = "11", Name = "Anbu", Address = "Chennai", Designation = "Technical Architect", DateOfJoining = "10/01/2015"  
  36.         });  
  37.         objEmployee.Add(new Employee {  
  38.             ID = "12", Name = "Moorthy", Address = "Delhi", Designation = "Project Manager", DateOfJoining = "10/01/2015"  
  39.         });  
  40.         objEmployee.Add(new Employee {  
  41.             ID = "13", Name = "Ganesh", Address = "Coimbatore", Designation = "Technical Lead", DateOfJoining = "10/07/2014"  
  42.         });  
  43.         objEmployee.Add(new Employee {  
  44.             ID = "14", Name = "Rahul", Address = "Madurai", Designation = "Technical Lead", DateOfJoining = "10/01/2015"  
  45.         });  
  46.         objEmployee.Add(new Employee {  
  47.             ID = "15", Name = "Sibeesh", Address = "Delhi", Designation = "Technical Architect", DateOfJoining = "10/08/2015"  
  48.         });  
  49.         objEmployee.Add(new Employee {  
  50.             ID = "16", Name = "Ravi", Address = "Chennai", Designation = "Project Manager", DateOfJoining = "10/01/2015"  
  51.         });  
  52.         objEmployee.Add(new Employee {  
  53.             ID = "17", Name = "Mahindra", Address = "Coimbatore", Designation = "Software Developer", DateOfJoining = "11/09/2015"  
  54.         });  
  55.         objEmployee.Add(new Employee {  
  56.             ID = "18", Name = "Arjun", Address = "Delhi", Designation = "Technical Architect", DateOfJoining = "10/01/2015"  
  57.         });  
  58.         objEmployee.Add(new Employee {  
  59.             ID = "19", Name = "Joe", Address = "Madurai", Designation = "Project Manager", DateOfJoining = "15/01/2015"  
  60.         });  
  61.         objEmployee.Add(new Employee {  
  62.             ID = "20", Name = "Martin", Address = "Chennai", Designation = "Technical Architect", DateOfJoining = "17/02/2015"  
  63.         });  
  64.     }  
  65. }  

Step 7

Right-click on top of the default Index action method and create View.cshtml.

The following image contains 7 steps, that are the entire procedure for getting data from the server side.

 

  1. Layout as null, which means I m not using any layout file for this page.

  2. We require a jQuery data table plug-in JavaScript & CSS files.

  3. Within the Data Table method, the “ServerSide” : ”True” property is explicitly saying that the data is coming from the server side.

  4. From the data table method we need to fire the Ajax request using the Ajax property.

  5. The Columns attribute contains the entire column collection from the data source, be sure your <th> header row count and Columns collection count match or else you will receive the jQuery data table error.

  6. Sometimes we might need to fire some piece of code before or after an Ajax request has been made, so we have two builtin events that will be very useful during the Ajax request. 

    I am going to prepare another set of articles that explain all the built-in events available inside the jQuery data tables.

    6.1. Xhr: After the completion of the Ajax request this event will be triggered but you need to hook up this event to the existing Data Table plug-in using the jQuery.On method.

    6.2. PreXhr: Before completing the Ajax request this event will is triggered.

  7. The table tblEmployee contains only <thead> tags, <tbody> should be empty only. While invoking the $("#tblEmployee").dataTable() method dynamically generate the <TR> tags based on the result set.

    code

Step 8

Within the JQueryDataTableController.cs controller, create another Ajax action method, JsonOutputMethod(), and paste the piece of code inside this method. 

Because this specific Ajax method is hardcoded inside the Ajax URL parameter in the jQuery Data table, the return type of this method is marked as an ActionResult base class. That means we are able to return both an Ajax and a normal response.

  1. "url"'@Url.Action("JsonOutputMethod")'  
  2.   
  3. public ActionResult JsonOutputMethod()  
  4. {  
  5.    if (Request.IsAjaxRequest())  
  6.    {  
  7.      return Json(new { draw = 1, recordsTotal = objEmployee.Count, recordsFiltered = 10,          data = objEmployee }, JsonRequestBehavior.AllowGet);  
  8.    }  
  9.    return View();  
  10. }  

Within this method, if the request should be Ajax then I will only return the entire List collection object to the View. I am getting the help from the Request.IsAjaxRequest(). 

Step 9

Open the RouteConfig.cs file within the App_Start folder, change the default route details (like change the controller name from Home to JQueryDataTable).

That’s it, you are good to go. If you browse the application you will be able to get the data from server side and the jQuery data table plug-in renders based on the server-side data.

I hope you guys enjoyed this a lot, after reading this article please provide your valuable comments. Your comments are highly appreciated.