Binding Grid To JSON In AngularJS, Using ASP.NET MVC 5

Introduction

In this article, we will learn how to bind data in jqxGrid plugin, using MVC5, AngularJS, and EntityFramework.

Prerequisites

As I said before, we are going to use jqxGrid plugin to display 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.

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

table

Create your MVC application

Now, open Visual Studio and select File >> New Project. Then, a new dialog will pop up with the name New Project. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

MVC application

Next, a new dialog will pop up for selecting the template. We are going choose MVC template and click OK.

MVC

After creating our project, we add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Framework, right click on the project name, click Add > Add New Item.
A dialog box will pop up. Inside Visual C#, select Data >> ADO.NET Entity Data Model, and enter name for your Dbcontext model as DbcontextCustomer.
Finally, click Add.

 Adding ADO.NET Entity Data Model

At this level, we are going to choose EF Designer from database, as show below.

EF Designer

After clicking Next button, a dialog will pop up with the name Connection Properties. You need to enter your server name and connect to a database panel, selecting database via dropdown List (Customer DB). Then, click OK.

 connection properties

 connection properties

Now, the dialog Entity Data Model wizard will pop up for choosing object which we need to use. In our case, we choose Customers table and click Finish. Finally, we see that EDMX model generates Customer class.

model

Create a Controller

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

Create a controller

Enter Controller name (‘CustomersController’).

Controller name

CustomersController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace AngularJS_Grid_Binding_To_JSON.Controllers  
  8. {  
  9.     public class CustomersController : Controller  
  10.     {  
  11.         //Dbcontext  
  12.         private CustomerDBEntities context = new CustomerDBEntities();  
  13.   
  14.   
  15.         public ActionResult Index()  
  16.         {  
  17.             return View();  
  18.         }  
  19.   
  20.         // GET: Customers  
  21.         public JsonResult GetCustomers()  
  22.         {  
  23.             var CustomersList = context.Customers.ToList();   
  24.   
  25.             return Json(CustomersList, JsonRequestBehavior.AllowGet);  
  26.         }  
  27.   
  28.   
  29.     }  
  30. }  
As you can see, I have created GetCustomers() action to retrieve the data from Customers table in JSON format.

Adding View

It’s easy to do. Just right click on Index() action, select Add View. A dialog box pops up. Write a name for your View and click Add.

Adding View

Note: Don’t forget to download the following libraries from jqxwidgets.
  1. <!-- CSS -->  
  2. <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  3. <!-- JS -->  
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  6. <script src="~/Scripts/jqxcore.js"></script>  
  7. <script src="~/Scripts/jqxdata.js"></script>  
  8. <script src="~/Scripts/jqxbuttons.js"></script>  
  9. <script src="~/Scripts/jqxcheckbox.js"></script>  
  10. <script src="~/Scripts/jqxgrid.js"></script>  
  11. <script src="~/Scripts/jqxgrid.selection.js"></script>  
  12. <script src="~/Scripts/jqxmenu.js"></script>  
  13. <script src="~/Scripts/jqxscrollbar.js"></script>  
  14. <script src="~/Scripts/jqxgrid.sort.js"></script>  
  15. <script src="~/Scripts/jqxgrid.columnsresize.js"></script>  
  16. <script src="~/Scripts/jqxangular.js"></script>  
  17. <script src="~/Scripts/demos.js"></script>  
Index cshtml
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5.   
  6.   
  7. @section scripts{  
  8.       
  9.     <!-- CSS -->  
  10.     <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  11.     <!-- JS -->  
  12.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  13.     <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  14.     <script src="~/Scripts/jqxcore.js"></script>  
  15.     <script src="~/Scripts/jqxdata.js"></script>  
  16.     <script src="~/Scripts/jqxbuttons.js"></script>  
  17.     <script src="~/Scripts/jqxcheckbox.js"></script>  
  18.     <script src="~/Scripts/jqxgrid.js"></script>  
  19.     <script src="~/Scripts/jqxgrid.selection.js"></script>  
  20.     <script src="~/Scripts/jqxmenu.js"></script>  
  21.     <script src="~/Scripts/jqxscrollbar.js"></script>  
  22.     <script src="~/Scripts/jqxgrid.sort.js"></script>  
  23.     <script src="~/Scripts/jqxgrid.columnsresize.js"></script>  
  24.     <script src="~/Scripts/jqxangular.js"></script>  
  25.     <script src="~/Scripts/demos.js"></script>  
  26.   
  27.     <script type="text/javascript">  
  28.   
  29.     var app = angular.module('myApp', ['jqwidgets']);  
  30.     app.controller('GridCtrl'function ($scope, $http) {  
  31.   
  32.         $scope.createWidget = false;  
  33.         $http({  
  34.   
  35.             method: 'GET',  
  36.             url: 'GetCustomers'  
  37.         }).success(function (data, status) {  
  38.   
  39.             // prepare the data  
  40.             var source = {  
  41.   
  42.                 datatype: "json",  
  43.                 datafields: [  
  44.                     { name: 'CustomerID', type: 'int' },  
  45.                     { name: 'CustomerName', type: 'string' },  
  46.                     { name: 'CustomerEmail', type: 'string' },  
  47.                     { name: 'CustomerZipCode', type: 'int' },  
  48.                     { name: 'CustomerCountry', type: 'string' },  
  49.                     { name: 'CustomerCity', type: 'string' }  
  50.                 ],  
  51.                 id: 'id',  
  52.                 localdata: data  
  53.             };  
  54.   
  55.             var dataAdapter = new $.jqx.dataAdapter(source);  
  56.             $scope.gridSettings =  
  57.             {  
  58.                 width: 950,  
  59.                 source: dataAdapter,  
  60.                 columnsresize: true,  
  61.   
  62.                 columns: [  
  63.                     { text: 'Customer ID', datafield: 'CustomerID', width: 250 },  
  64.                     { text: 'Customer Name', datafield: 'CustomerName', width: 250 },  
  65.                     { text: 'Customer Email', datafield: 'CustomerEmail', width: 250 },  
  66.                     { text: 'Customer ZipCode', datafield: 'CustomerZipCode', width: 250 },  
  67.                     { text: 'Customer Country', datafield: 'CustomerCountry', width: 250 },  
  68.                     { text: 'Customer City', datafield: 'CustomerCity', width: 250 }  
  69.   
  70.                 ]  
  71.   
  72.             };  
  73.             //now create the widget.  
  74.             $scope.createWidget = true;  
  75.   
  76.         }).error(function (data, status) {  
  77.   
  78.             console.log('Something Wrong');  
  79.         });  
  80.   
  81.     });  
  82.   
  83.   
  84. </script>  
  85.       
  86.     }   
  87.   
  88. <h2>AngularJS Grid Binding To JSON</h2>  
  89.   
  90. <div ng-app="myApp" ng-controller="GridCtrl">  
  91.     <jqx-grid jqx-create="createWidget" jqx-settings="gridSettings"></jqx-grid>  
  92. </div>  
Output

 

Output


Similar Articles