Data Binding To JSON Data In AngularJS Datatable

Introduction

In this post, you will see how to bind data in datatable plugin using MVC5, AngularJS, and EntityFramework, JSON.

Prerequisites

As I said earlier, we are going to use datatable plugin to display the data in our MVC application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

SQL Database part

Here, you can find the scripts to create database and table.

Create Database

  1. USE[master]    
  2. GO    
  3. /****** Object: Database [CustomerDB] Script Date: 9/11/2016 4:54:28 AM ******/    
  4. CREATE DATABASE[CustomerDB]    
  5. CONTAINMENT = NONE    
  6. ON PRIMARY    
  7.     (NNAME = N 'CustomerDB', FILENAME = N 'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf'SIZE = 3072 KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024 KB)    
  8. LOG ON    
  9.     (NNAME = N 'CustomerDB_log', FILENAME = N 'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB_log.ldf'SIZE = 1024 KB, MAXSIZE = 2048 GB, FILEGROWTH = 10 % )    
  10. GO    
  11. ALTER DATABASE[CustomerDB] SET COMPATIBILITY_LEVEL = 110    
  12. GO    
  13. IF(1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))    
  14. begin    
  15. EXEC[CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'    
  16. end    
  17. GO    
  18. ALTER DATABASE[CustomerDB] SET ANSI_NULL_DEFAULT OFF    
  19. GO    
  20. ALTER DATABASE[CustomerDB] SET ANSI_NULLS OFF    
  21. GO    
  22. ALTER DATABASE[CustomerDB] SET ANSI_PADDING OFF    
  23. GO    
  24. ALTER DATABASE[CustomerDB] SET ANSI_WARNINGS OFF    
  25. GO    
  26. ALTER DATABASE[CustomerDB] SET ARITHABORT OFF    
  27. GO    
  28. ALTER DATABASE[CustomerDB] SET AUTO_CLOSE OFF    
  29. GO    
  30. ALTER DATABASE[CustomerDB] SET AUTO_CREATE_STATISTICS ON    
  31. GO    
  32. ALTER DATABASE[CustomerDB] SET AUTO_SHRINK OFF    
  33. GO    
  34. ALTER DATABASE[CustomerDB] SET AUTO_UPDATE_STATISTICS ON    
  35. GO    
  36. ALTER DATABASE[CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF    
  37. GO    
  38. ALTER DATABASE[CustomerDB] SET CURSOR_DEFAULT GLOBAL    
  39. GO    
  40. ALTER DATABASE[CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF    
  41. GO    
  42. ALTER DATABASE[CustomerDB] SET NUMERIC_ROUNDABORT OFF    
  43. GO    
  44. ALTER DATABASE[CustomerDB] SET QUOTED_IDENTIFIER OFF    
  45. GO    
  46. ALTER DATABASE[CustomerDB] SET RECURSIVE_TRIGGERS OFF    
  47. GO    
  48. ALTER DATABASE[CustomerDB] SET DISABLE_BROKER    
  49. GO    
  50. ALTER DATABASE[CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF    
  51. GO    
  52. ALTER DATABASE[CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF    
  53. GO    
  54. ALTER DATABASE[CustomerDB] SET TRUSTWORTHY OFF    
  55. GO    
  56. ALTER DATABASE[CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF    
  57. GO    
  58. ALTER DATABASE[CustomerDB] SET PARAMETERIZATION SIMPLE    
  59. GO    
  60. ALTER DATABASE[CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF    
  61. GO    
  62. ALTER DATABASE[CustomerDB] SET HONOR_BROKER_PRIORITY OFF    
  63. GO    
  64. ALTER DATABASE[CustomerDB] SET RECOVERY SIMPLE    
  65. GO    
  66. ALTER DATABASE[CustomerDB] SET MULTI_USER    
  67. GO    
  68. ALTER DATABASE[CustomerDB] SET PAGE_VERIFY CHECKSUM    
  69. GO    
  70. ALTER DATABASE[CustomerDB] SET DB_CHAINING OFF    
  71. GO    
  72. ALTER DATABASE[CustomerDB] SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF)    
  73. GO    
  74. ALTER DATABASE[CustomerDB] SET TARGET_RECOVERY_TIME = 0 SECONDS    
  75. GO    
  76. ALTER DATABASE[CustomerDB] SET READ_WRITE    
  77. GO    
Create Table
  1. USE[CustomerDB]  
  2. GO  
  3. /****** Object: Table [dbo].[Customers] Script Date: 9/11/2016 4:53:43 AM ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6. SET QUOTED_IDENTIFIER ON  
  7. GO  
  8. SET ANSI_PADDING ON  
  9. GO  
  10. CREATE TABLE[dbo].[Customers](  
  11.     [CustomerID][int] NOT NULL, [CustomerName][varchar](50) NULL, [CustomerEmail][varchar](50) NULL, [CustomerZipCode][int] NULL, [CustomerCountry][varchar](50) NULL, [CustomerCity][varchar](50) NULL,  
  12.     CONSTRAINT[PK_Customers] PRIMARY KEY CLUSTERED(  
  13.         [CustomerID] ASC  
  14.     ) WITH(PAD_INDEX = OFFSTATISTICS_NORECOMPUTE = OFFIGNORE_DUP_KEY = OFFALLOW_ROW_LOCKS = ONALLOW_PAGE_LOCKS = ON) ON[PRIMARY]  
  15. ) ON[PRIMARY]  
  16. GO  
  17. SET ANSI_PADDING OFF GO  
After creating the table, you can add some records.

creating

Create your MVC application

Open Visual Studio and select File.
 
Click New Project.
 
A new dialog box  will pop up with the name New Project. Select ASP.NET Web Application (.NET Framework).
 
Name your project and click OK.

creating

The next step is to select template. In this example, we need to choose MVC template and click OK.

creating

After creating the project, it’s time to add ADO.NET Entity Data Model.
 
Let’s Go.

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Framework, right click on the project name.
 
Click Add >> Add New Item.
 
A new dialog box will open. Inside Visual C#, select Data >> ADO.NET Entity Data Model.
 
Enter the name for your Dbcontext model as DbContextCustomer.
 
Finally, click Add.



Now, we are going to choose EF Designer from database, as show below.



After clicking Next button, a new dialog will pop up with the name connection properties. You need to enter your server name.
 
In order to connect to a database panel, select the desired database via dropdown List (Customer DB).
 
Then, click OK.



creating

In the final step, the Entity Data Model Wizard will pop up for choosing object which we want to use.
 
In our case, we are going to choose Customers table and click Finish.
 
Finally, we see that EDMX model generates Customers class.



Make sure that our EDMX file has added in our project, as given below.



Create a Controller

Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> select "MVC5 Controller – Empty" > click Add.

creating

Enter Controller name (‘CustomersController’).



CustomersController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace ANGULAR_DATATABLE.Controllers {  
  7.     public class CustomersController: Controller {  
  8.         //Db Context  
  9.         private CustomerDBEntities context = new CustomerDBEntities();  
  10.   
  11.         // GET: Customers  
  12.         public ActionResult Index() {  
  13.             return View();  
  14.         }  
  15.         public JsonResult GetCustomerList() {  
  16.             var CustomerList = context.Customers.ToList();  
  17.             return Json(CustomerList, JsonRequestBehavior.AllowGet);  
  18.         }  
  19.     }  
  20. }  
As you can see, I am creating GetCustomers() action to retrieve data from Customers table in JSON format.

Adding View

For adding View, just right click on Index() action, select Add View. A new dialog will pop up. Write a name for your View, and click Add.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace ANGULAR_DATATABLE.Controllers {     public class CustomersController: Controller {         //Db Context         private CustomerDBEntities context = new CustomerDBEntities();          // GET: Customers         public ActionResult Index() {             return View();         }         public JsonResult GetCustomerList() {             var CustomerList = context.Customers.ToList();             return Json(CustomerList, JsonRequestBehavior.AllowGet);         }     } }

Note

Don’t forget to download the following libraries from jqxwidgets.
  1. <!-- CSS -->  
  2. <  
  3. link href = "~/Content/jqx.base.css"  
  4. rel = "stylesheet" / >  
  5.     <!-- JS -->  
  6.     <  
  7.     script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js" > < /script> <  
  8.     script src = "~/Scripts/jqxcore.js" > < /script> <  
  9.     script src = "~/Scripts/jqxangular.js" > < /script> <  
  10.     script src = "~/Scripts/jqxbuttons.js" > < /script> <  
  11.     script src = "~/Scripts/jqxscrollbar.js" > < /script> <  
  12.     script src = "~/Scripts/jqxdata.js" > < /script> <  
  13.     script src = "~/Scripts/jqxdatatable.js" > < /script> <  
  14.     script src = "~/Scripts/demos.js" > < /script>  
Index cshtml
  1. @ {  
  2.     ViewBag.Title = "CustomerView";  
  3. }  
  4. @section scripts {  
  5.     <!-- CSS -->  
  6.     <  
  7.     link href = "~/Content/jqx.base.css"  
  8.     rel = "stylesheet" / >  
  9.         <!-- JS -->  
  10.         <  
  11.         script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js" > < /script> <  
  12.         script src = "~/Scripts/jqxcore.js" > < /script> <  
  13.         script src = "~/Scripts/jqxangular.js" > < /script> <  
  14.         script src = "~/Scripts/jqxbuttons.js" > < /script> <  
  15.         script src = "~/Scripts/jqxscrollbar.js" > < /script> <  
  16.         script src = "~/Scripts/jqxdata.js" > < /script> <  
  17.         script src = "~/Scripts/jqxdatatable.js" > < /script> <  
  18.         script src = "~/Scripts/demos.js" > < /script> <  
  19.         script type = "text/javascript" >  
  20.         var myApp = angular.module('myApp', ['jqwidgets']);  
  21.     myApp.controller('DataTableCtrl', ['$scope', function($scope) {  
  22.         $scope.gridSettings = {  
  23.             width: 850,  
  24.             pageable: true,  
  25.             pagerButtonsCount: 10,  
  26.             source: new $.jqx.dataAdapter({  
  27.                 dataType: "json",  
  28.                 dataFields: [{  
  29.                     name: 'CustomerName',  
  30.                     type: 'string'  
  31.                 }, {  
  32.                     name: 'CustomerEmail',  
  33.                     type: 'string'  
  34.                 }, {  
  35.                     name: 'CustomerZipCode',  
  36.                     type: 'int'  
  37.                 }, {  
  38.                     name: 'CustomerCountry',  
  39.                     type: 'string'  
  40.                 }, {  
  41.                     name: 'CustomerCity',  
  42.                     type: 'string'  
  43.                 }],  
  44.                 id: 'CustomerID',  
  45.                 url: 'GetCustomerList'  
  46.             }),  
  47.             columnsResize: true,  
  48.             columns: [{  
  49.                 text: 'CustomerName',  
  50.                 dataField: 'CustomerName',  
  51.                 width: 300  
  52.             }, {  
  53.                 text: 'CustomerEmail',  
  54.                 dataField: 'CustomerEmail',  
  55.                 width: 300  
  56.             }, {  
  57.                 text: 'CustomerZipCode',  
  58.                 dataField: 'CustomerZipCode',  
  59.                 width: 300  
  60.             }, {  
  61.                 text: 'CustomerCountry',  
  62.                 dataField: 'CustomerCountry',  
  63.                 width: 300  
  64.             }, {  
  65.                 text: 'CustomerCity',  
  66.                 dataField: 'CustomerCity',  
  67.                 width: 300  
  68.             }]  
  69.         };  
  70.     }]); <  
  71.     /script>  
  72. <  
  73. div ng - app = "myApp"  
  74. ng - controller = "DataTableCtrl" >  
  75.     <  
  76.     h2 > Data Binding to JSON data in AngularJS DataTable < /h2> <  
  77.     jqx - data - table jqx - settings = "gridSettings" > < /jqx-data-table> <  
  78.     /div>  
Output

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now