Fetch Records In Angular Datatable Using Entity Framework And Bootstrap

In this session, I will show you the steps of implementing Angular Datatable in ASP.NET MVC application. There is no need for adding filter data and pagination manually. You can get all these benefits by using Datatable which is open source. 
DataTables is a powerful and easy to use jQuery plugin for displaying tabular data with features for Pagination, searching, State saving, Multi-column sorting with data type detection and lots more with ZERO or minimal configuration. For resolving the datatable performance issue like server-side filtering, sorting, and paging features when working with a large amount of data, many Angular grid tools are available.
Before you go through this session I will suggest you visit my previous Datatable session as mentioned below. As I have used the same table structure with the data and same Entity Data Model in both sessions and under the same project I have created both these datatable based applications.
Steps to be followed,
Step 1
Add required JS, CSS files and 3 image files for sorting icons in the application that are the dependencies of angular-datatables.
You can find  these files from the attachment and the structure should look like as mentioned below.
Fetch Records In Angular Datatable Using Entity Framework And Bootstrap
Step 2
Here, I have added a JavaScript file in the "Scripts" folder for adding Angular components (module, controller etc). Right-click on your "Scripts" folder > Add > New Item > select "javascript" file > Enter name (here "myApp.js")> Ok.
Code Ref
  1. var app = angular.module('MyApp', ['datatables']);  
  2. app.controller('homeCtrl', ['$scope''$http''DTOptionsBuilder''DTColumnBuilder',  
  3.     function ($scope, $http, DTOptionsBuilder, DTColumnBuilder) {  
  4.         $scope.dtColumns = [  
  5.             DTColumnBuilder.newColumn("FirstName""First Name"),  
  6.             DTColumnBuilder.newColumn("LastName""Last Name"),  
  7.             DTColumnBuilder.newColumn("Age""Age"),  
  8.             DTColumnBuilder.newColumn("Address""Address"),  
  9.             DTColumnBuilder.newColumn("City""City"),  
  10.             DTColumnBuilder.newColumn("State""State")  
  11.         ]  
  13.         $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {  
  14.             url: "/home/getdata",  
  15.             type: "POST"  
  16.         })  
  17.             .withPaginationType('full_numbers')  
  18.             .withDisplayLength(10);  
  19.     }])  
Code Description
I have included the datatables module in our module (here "MyApp") for implementing datatables in our AngularJS application.
  1. var app = angular.module('MyApp', ['datatables']);  
Here, I have defined some function parameters with the controller name.
  1. app.controller('homeCtrl', ['$scope''$http''DTOptionsBuilder''DTColumnBuilder',  
  2.     function ($scope, $http, DTOptionsBuilder, DTColumnBuilder)   

I have mentioned the columns of table (Entity Types) as uploaded using enitity data model using scope object. The scope is a JavaScript object which basically binds the "controller" and the "view". One can define member variables in the scope within the controller which can then be accessed by the view.

  1. $scope.dtColumns = [  
  2.             DTColumnBuilder.newColumn("FirstName""First Name"),  
  3.             DTColumnBuilder.newColumn("LastName""Last Name"),  
  4.             DTColumnBuilder.newColumn("Age""Age"),  
  5.             DTColumnBuilder.newColumn("Address""Address"),  
  6.             DTColumnBuilder.newColumn("City""City"),  
  7.             DTColumnBuilder.newColumn("State""State")  
  8.         ]  
Here, I have another scope object which specified the controller action method getdata to load data from the backend using Entity data model.
  1. $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {  
  2.             url: "/home/getdata",  
  3.             type: "POST"  
  4.         })  
Here, I have mentioned UI pagination type with the no. of records per page.
  1. .withPaginationType('full_numbers')  
  2.             .withDisplayLength(10);  
Step 3
Here, I have added "Angular" Action into "Home" Controller. Please write the following code.
  1. public ActionResult Angular()  
  2.         {  
  3.             return View();  
  4.         }  
Step 4
Add another action into your controller for getting data from a database for showing in the datatables.
  1. public ActionResult getData()  
  2.         {  
  3.             using (SatyaDBEntities dc = new SatyaDBEntities())  
  4.             {  
  5.                 return Json(dc.employees.ToList());  
  6.             }  
  7.         }  
Step 5
Add view for your Action & design for showing the data in the datatables.
Right-click on Action Method (here right-click on Angular action) > Add View... > Enter View Name > Select View Engine (Razor) > Add.
Code Ref
  1. @{  
  2.     ViewBag.Title = "Angular Datatables";  
  3. }  
  5. <h2 style="color: blue">Satyaprakash-Angular Datatable Using EF and Bootstrap</h2>  
  8. <link href="~/css/bootstrap.css" rel="stylesheet" />  
  9. <link href="~/css/jquery.dataTables.min.css" rel="stylesheet" />  
  11. <script src="~/Scripts/jquery.js"></script>  
  12. <script src="~/Scripts/jquery.dataTables.js"></script>  
  13. <script src="~/Scripts/angular.js"></script>  
  14. <script src="~/Scripts/angular-datatables.js"></script>  
  16. <script src="~/Scripts/myApp.js"></script>  
  19. <div ng-app="MyApp" class="container">  
  20.     <div ng-controller="homeCtrl">  
  21.         <table id="entry-grid" datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-hover"></table>  
  22.     </div>  
  23. </div>  
Code Description
For datatable CSS
  1. <link href="~/css/bootstrap.css" rel="stylesheet" />  
  2. <link href="~/css/jquery.dataTables.min.css" rel="stylesheet" />  
JS for AngularJS and Datatable 
  1. <script src="~/Scripts/jquery.js"></script>  
  2. <script src="~/Scripts/jquery.dataTables.js"></script>  
  3. <script src="~/Scripts/angular.js"></script>  
  4. <script src="~/Scripts/angular-datatables.js"></script>  
JS for our AngularJS module, controller. 
  1. <script src="~/Scripts/myApp.js"></script>  
Here, in div tag inside HTML element I have added Angular module with controller name. 
  1. <div ng-app="MyApp" class="container">  
  2.     <div ng-controller="homeCtrl">  
Then in the table element I have mentioned Angular scope objects, that is dtOptions and dtColumns, with CSS class as described in the earlier step.
  1. <div ng-app="MyApp" class="container">  
  2.     <div ng-controller="homeCtrl">  
  3.         <table id="entry-grid" datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-hover"></table>  
  4.     </div>  
  5. </div>  
During Page load initially.
Fetch Records In Angular Datatable Using Entity Framework And Bootstrap
It supports Pagination, searching, State saving, and Multi-column sorting with data type detection.
Fetch Records In Angular Datatable Using Entity Framework And Bootstrap
  • Implement the Angular Datatable using MVC and Bootstrap.
  • Datatable with Bootstrap using Angular DataTables dependencies.
  • Features of Datatable makes coding easier.
  • Datatable using Entity Framework.