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.
Create a table named the employee. I have added its script including schema with data.
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.
- public ActionResult Index()
- {
- return View();
- }
Step 4
I added another action method named loaddata for fetching data from the database using entity framework.
Code Ref
- public ActionResult loaddata()
- {
- using (SatyaDBEntities dc = new SatyaDBEntities())
- {
- var data = dc.employees.OrderBy(a => a.FirstName).ToList();
- return Json(new { data = data }, JsonRequestBehavior.AllowGet);
- }
- }
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.
- dc.Configuration.LazyLoadingEnabled = false;
Step 5
I have added a view for the action Index & design.
Code ref
- @{
- ViewBag.Title = "Index";
- }
-
- <h2 style="color: blue">Satyaprakash-jQuery Datatable Using ASP.NET MVC</h2>
-
- <style>
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
-
- tr:nth-child(even) {
- background-color: #dddddd;
- }
- </style>
-
- <div style="width:90%; margin:0 auto;">
- <table id="myTable" align="center" border="1" cellpadding="4" cellspacing="4">
- <thead>
- <tr>
- <th style="background-color: Yellow;color: blue">First Name</th>
- <th style="background-color: Yellow;color: blue">Last Name</th>
- <th style="background-color: Yellow;color: blue">Age</th>
- <th style="background-color: Yellow;color: blue">Address</th>
- <th style="background-color: Yellow;color: blue">City</th>
- <th style="background-color: Yellow;color: blue">State</th>
- </tr>
- </thead>
- </table>
- </div>
- <style>
- tr.even {
- background-color: #F5F5F5 !important;
- }
- </style>
- @* Load bootstrap datatable css *@
- <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" />
- <link href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" />
-
- @* Load normal datatable css *@
- @*<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />*@
-
- @* Load bootstrap datatable js *@
- @section Scripts{
- <script src="//code.jquery.com/jquery-3.3.1.js"></script>
- <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
- <script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
-
- @* Load normal datatable js *@
- @*<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>*@
-
- <script>
- $(document).ready(function () {
- $('#myTable').DataTable({
- "ajax": {
- "url": "/home/loaddata",
- "type": "GET",
- "datatype": "json"
- },
- "columns": [
- { "data": "FirstName", "autoWidth": true },
- { "data": "LastName", "autoWidth": true },
- { "data": "Age", "autoWidth": true },
- { "data": "Address", "autoWidth": true },
- { "data": "City", "autoWidth": true },
- { "data": "State", "autoWidth": true }
- ]
- });
- });
- </script>
- }
Code Description
I can add datatable CDN reference to implement normal Datatable CSS and Datatable JS.
- <link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />
- <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.
- <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" />
- <link href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" />
- <script src="//code.jquery.com/jquery-3.3.1.js"></script>
- <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
- <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.
- <script>
- $(document).ready(function () {
- $('#myTable').DataTable({
- "ajax": {
- "url": "/home/loaddata",
- "type": "GET",
- "datatype": "json"
- },
- "columns": [
- { "data": "FirstName", "autoWidth": true },
- { "data": "LastName", "autoWidth": true },
- { "data": "Age", "autoWidth": true },
- { "data": "Address", "autoWidth": true },
- { "data": "City", "autoWidth": true },
- { "data": "State", "autoWidth": true }
- ]
- });
- });
- </script>
OUTPUT
During Page load initially.
It supports Pagination, searching, State saving, Multi-column sorting with data type detection.
SUMMARY
- Implement jQuery Datatable using MVC and Bootstrap.
- Datatable with Bootstrap using CDN reference.
- Features of Datatable makes coding easier.
- Datatable using Entity Framework.