Retrieve Records In jQuery Datatable Using Entity Framework And Bootstrap

Introduction

In this session, I will show you the steps to Implement jQuery Datatable in ASP.NET MVC application. There is no need to add filter data and pagination manually. You can get all these benefits by using Datatable. Datatable is open source.
 

Description

DataTable is a most powerful and easy to use jQuery plugin for displaying tabular data with features for Pagination, searching, State saving, and Multi-column sorting with data type detection and lots more with ZERO or minimal configuration.
 
Note
Before going through the session, I will suggest you to first visit the previous MVC related articles with backend knowledge.
To know more about Datatable visit the Datatable official site.
Steps to be followed,
 
Step 1
 
Create a table named the employee. I have added its script including schema with data.
  1. CREATE TABLE [dbo].[employee](  
  2.     [FirstName] [varchar](15) NOT NULL,  
  3.     [LastName] [varchar](20) NULL,  
  4.     [Age] [intNULL,  
  5.     [Address] [varchar](30) NULL,  
  6.     [City] [varchar](20) NULL,  
  7.     [State] [varchar](20) NULL,  
  8.  CONSTRAINT [PK_employee] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [FirstName] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13. GO  
  14. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Ajit', N'Mukesh', 36, N'2130 Boars Nest', N'Hazard Co', N'Georgia')  
  15. GO  
  16. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Ashwini', N'Bajaj', 29, N'2130 Boars Nest', N'Hazard Co', N'Georgia')  
  17. GO  
  18. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Beenna', N'Malik', 42, N'2130 Boars Nest', N'Hazard Co', N'Georgia')  
  19. GO  
  20. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Keshav', N'Malhotra', 26, N'Noida, Sector-12', N'Delhi', N'Uttar Pradesh')  
  21. GO  
  22. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Kulu', N'Samantaray', 38, N'Ghaziabad', N'Ghaziabad', N'Uttar Pradesh')  
  23. GO  
  24. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Raghav', N'Manoj', 27, N'2130 Boars Nest', N'Hazard Co', N'Georgia')  
  25. GO  
  26. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Ram', N'Prasad', 24, N'Hyd, NY', N'Hyderabad', N'Andhra Pradesh')  
  27. GO  
  28. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Satyaprakash', N'Samantaray', 27, N'Khordha, Bhubaneswar', N'Khordha', N'Odisha')  
  29. GO  
  30. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Sita', N'Goswami', 21, N'HSR Layout', N'Bangalore', N'Karnataka')  
  31. GO  
  32. INSERT [dbo].[employee] ([FirstName], [LastName], [Age], [Address], [City], [State]) VALUES (N'Venkat', N'Mukherjee', 34, N'Koramangala', N'Bengaluru', N'Karnataka')  
  33. GO  
Step 2
 
Added Entity Data Model named SatyaDBModel.edmx.
 
Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add. A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables (employee) > enter Model Namespace > Finish.
 
Step 3
 
Here, I have taken the index action method of the home controller to get the view where we will implement jQuery DataTable.
  1. public ActionResult Index()  
  2.         {  
  3.             return View();  
  4.         }  
Step 4
 
I added another action method named loaddata for fetching data from the database using entity framework.
 
Code Ref
  1. public ActionResult loaddata()  
  2.         {  
  3.             using (SatyaDBEntities dc = new SatyaDBEntities())  
  4.             {   
  5.                 var data = dc.employees.OrderBy(a => a.FirstName).ToList();  
  6.                 return Json(new { data = data }, JsonRequestBehavior.AllowGet);  
  7.             }  
  8.         }  
Code Description
 
Here, SatyaDBEntities is the name of the model namespace. Using the object of model namespace we can access the database object and its property.
 
If the database object (table) contains foreign key then you have to add the below line of code.
  1. dc.Configuration.LazyLoadingEnabled = false;  
Step 5
 
I have added a view for the action Index & design. 
 
Code ref
  1. @{  
  2.      ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2 style="color: blue">Satyaprakash-jQuery Datatable Using ASP.NET MVC</h2>  
  6.   
  7. <style>  
  8.     table {  
  9.         font-family: arial, sans-serif;  
  10.         border-collapse: collapse;  
  11.         width: 100%;  
  12.     }  
  13.   
  14.     td, th {  
  15.         border: 1px solid #dddddd;  
  16.         text-align: left;  
  17.         padding: 8px;  
  18.     }  
  19.   
  20.     tr:nth-child(even) {  
  21.         background-color: #dddddd;  
  22.     }  
  23. </style>  
  24.   
  25. <div style="width:90%; margin:0 auto;">  
  26.     <table id="myTable" align="center" border="1" cellpadding="4" cellspacing="4">  
  27.         <thead>  
  28.             <tr>  
  29.                 <th style="background-color: Yellow;color: blue">First Name</th>  
  30.                 <th style="background-color: Yellow;color: blue">Last Name</th>  
  31.                 <th style="background-color: Yellow;color: blue">Age</th>  
  32.                 <th style="background-color: Yellow;color: blue">Address</th>  
  33.                 <th style="background-color: Yellow;color: blue">City</th>  
  34.                 <th style="background-color: Yellow;color: blue">State</th>  
  35.             </tr>  
  36.         </thead>  
  37.     </table>  
  38. </div>  
  39. <style>  
  40.     tr.even {  
  41.         background-color: #F5F5F5 !important;  
  42.     }  
  43. </style>  
  44. @* Load bootstrap datatable css *@  
  45. <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" />  
  46. <link href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" />  
  47.   
  48. @* Load normal datatable css *@  
  49. @*<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />*@  
  50.   
  51. @* Load bootstrap datatable js *@  
  52. @section Scripts{  
  53.     <script src="//code.jquery.com/jquery-3.3.1.js"></script>  
  54.     <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>  
  55.     <script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>  
  56.   
  57.     @* Load normal datatable js *@  
  58.     @*<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>*@  
  59.   
  60.     <script>  
  61.         $(document).ready(function () {  
  62.             $('#myTable').DataTable({  
  63.                 "ajax": {  
  64.                     "url""/home/loaddata",  
  65.                     "type""GET",  
  66.                     "datatype""json"  
  67.                 },  
  68.                 "columns": [  
  69.                     { "data""FirstName""autoWidth"true },  
  70.                     { "data""LastName""autoWidth"true },  
  71.                     { "data""Age""autoWidth"true },  
  72.                     { "data""Address""autoWidth"true },  
  73.                     { "data""City""autoWidth"true },  
  74.                     { "data""State""autoWidth"true }  
  75.                 ]  
  76.             });  
  77.         });  
  78.     </script>  
  79. }  
Code Description
 
I can add datatable CDN reference to implement normal Datatable CSS and Datatable JS.
  1. <link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />  
  1. <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>  
But here, I have added datatable CDN reference to implement bootstrap Datatable CSS and Datatable JS. 
  1. <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" />  
  2. <link href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" />  
  1. <script src="//code.jquery.com/jquery-3.3.1.js"></script>  
  2.     <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>  
  3.     <script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>   
Then I have added jQuery to load records and fetch into the datatable. Here, myTable is the ID of HTML table and in the URL section I have added the Home controller with controller action method to fetch records and in the columns section it contains all the properties of database objects.
  1. <script>  
  2.         $(document).ready(function () {  
  3.             $('#myTable').DataTable({  
  4.                 "ajax": {  
  5.                     "url""/home/loaddata",  
  6.                     "type""GET",  
  7.                     "datatype""json"  
  8.                 },  
  9.                 "columns": [  
  10.                     { "data""FirstName""autoWidth"true },  
  11.                     { "data""LastName""autoWidth"true },  
  12.                     { "data""Age""autoWidth"true },  
  13.                     { "data""Address""autoWidth"true },  
  14.                     { "data""City""autoWidth"true },  
  15.                     { "data""State""autoWidth"true }  
  16.                 ]  
  17.             });  
  18.         });  
  19.     </script>  

OUTPUT

During Page load initially.
 
Retrieve Records In jQuery Datatable Using Entity Framework And Bootstrap
 
It supports Pagination, searching, State saving, Multi-column sorting with data type detection.
 
Retrieve Records In jQuery Datatable Using Entity Framework And Bootstrap

SUMMARY

 
  • Implement jQuery Datatable using MVC and Bootstrap.
  • Datatable with Bootstrap using CDN reference.
  • Features of Datatable makes coding easier.
  • Datatable using Entity Framework.