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. /****** Object: Database [CustomerDB] Script Date: 9/11/2016 4:54:28 AM ******/
  5. CREATE DATABASE [CustomerDB]
  6. CONTAINMENT = NONE
  7. ON PRIMARY
  8. ( NAME = N'CustomerDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  9. LOG ON
  10. ( 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%)
  11. GO
  12. ALTER DATABASE [CustomerDB] SET COMPATIBILITY_LEVEL = 110
  13. GO
  14. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  15. begin
  16. EXEC [CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'
  17. end
  18. GO
  19. ALTER DATABASE [CustomerDB] SET ANSI_NULL_DEFAULT OFF
  20. GO
  21. ALTER DATABASE [CustomerDB] SET ANSI_NULLS OFF
  22. GO
  23. ALTER DATABASE [CustomerDB] SET ANSI_PADDING OFF
  24. GO
  25. ALTER DATABASE [CustomerDB] SET ANSI_WARNINGS OFF
  26. GO
  27. ALTER DATABASE [CustomerDB] SET ARITHABORT OFF
  28. GO
  29. ALTER DATABASE [CustomerDB] SET AUTO_CLOSE OFF
  30. GO
  31. ALTER DATABASE [CustomerDB] SET AUTO_CREATE_STATISTICS ON
  32. GO
  33. ALTER DATABASE [CustomerDB] SET AUTO_SHRINK OFF
  34. GO
  35. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS ON
  36. GO
  37. ALTER DATABASE [CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF
  38. GO
  39. ALTER DATABASE [CustomerDB] SET CURSOR_DEFAULT GLOBAL
  40. GO
  41. ALTER DATABASE [CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF
  42. GO
  43. ALTER DATABASE [CustomerDB] SET NUMERIC_ROUNDABORT OFF
  44. GO
  45. ALTER DATABASE [CustomerDB] SET QUOTED_IDENTIFIER OFF
  46. GO
  47. ALTER DATABASE [CustomerDB] SET RECURSIVE_TRIGGERS OFF
  48. GO
  49. ALTER DATABASE [CustomerDB] SET DISABLE_BROKER
  50. GO
  51. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  52. GO
  53. ALTER DATABASE [CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF
  54. GO
  55. ALTER DATABASE [CustomerDB] SET TRUSTWORTHY OFF
  56. GO
  57. ALTER DATABASE [CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
  58. GO
  59. ALTER DATABASE [CustomerDB] SET PARAMETERIZATION SIMPLE
  60. GO
  61. ALTER DATABASE [CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF
  62. GO
  63. ALTER DATABASE [CustomerDB] SET HONOR_BROKER_PRIORITY OFF
  64. GO
  65. ALTER DATABASE [CustomerDB] SET RECOVERY SIMPLE
  66. GO
  67. ALTER DATABASE [CustomerDB] SET MULTI_USER
  68. GO
  69. ALTER DATABASE [CustomerDB] SET PAGE_VERIFY CHECKSUM
  70. GO
  71. ALTER DATABASE [CustomerDB] SET DB_CHAINING OFF
  72. GO
  73. ALTER DATABASE [CustomerDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  74. GO
  75. ALTER DATABASE [CustomerDB] SET TARGET_RECOVERY_TIME = 0 SECONDS
  76. GO
  77. ALTER DATABASE [CustomerDB] SET READ_WRITE
  78. 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,
  12. [CustomerName] [varchar](50) NULL,
  13. [CustomerEmail] [varchar](50) NULL,
  14. [CustomerZipCode] [int] NULL,
  15. [CustomerCountry] [varchar](50) NULL,
  16. [CustomerCity] [varchar](50) NULL,
  17. CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
  18. (
  19. [CustomerID] ASC
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  21. ) ON [PRIMARY]
  22. GO
  23. SET ANSI_PADDING OFF
  24. 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. namespace AngularJS_Grid_Binding_To_JSON.Controllers
  7. {
  8. public class CustomersController : Controller
  9. {
  10. //Dbcontext
  11. private CustomerDBEntities context = new CustomerDBEntities();
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. // GET: Customers
  17. public JsonResult GetCustomers()
  18. {
  19. var CustomersList = context.Customers.ToList();
  20. return Json(CustomersList, JsonRequestBehavior.AllowGet);
  21. }
  22. }
  23. }
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. @section scripts{
  5. <!-- CSS -->
  6. <link href="~/Content/jqx.base.css" rel="stylesheet" />
  7. <!-- JS -->
  8. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  9. <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  10. <script src="~/Scripts/jqxcore.js"></script>
  11. <script src="~/Scripts/jqxdata.js"></script>
  12. <script src="~/Scripts/jqxbuttons.js"></script>
  13. <script src="~/Scripts/jqxcheckbox.js"></script>
  14. <script src="~/Scripts/jqxgrid.js"></script>
  15. <script src="~/Scripts/jqxgrid.selection.js"></script>
  16. <script src="~/Scripts/jqxmenu.js"></script>
  17. <script src="~/Scripts/jqxscrollbar.js"></script>
  18. <script src="~/Scripts/jqxgrid.sort.js"></script>
  19. <script src="~/Scripts/jqxgrid.columnsresize.js"></script>
  20. <script src="~/Scripts/jqxangular.js"></script>
  21. <script src="~/Scripts/demos.js"></script>
  22. <script type="text/javascript">
  23. var app = angular.module('myApp', ['jqwidgets']);
  24. app.controller('GridCtrl', function ($scope, $http) {
  25. $scope.createWidget = false;
  26. $http({
  27. method: 'GET',
  28. url: 'GetCustomers'
  29. }).success(function (data, status) {
  30. // prepare the data
  31. var source = {
  32. datatype: "json",
  33. datafields: [
  34. { name: 'CustomerID', type: 'int' },
  35. { name: 'CustomerName', type: 'string' },
  36. { name: 'CustomerEmail', type: 'string' },
  37. { name: 'CustomerZipCode', type: 'int' },
  38. { name: 'CustomerCountry', type: 'string' },
  39. { name: 'CustomerCity', type: 'string' }
  40. ],
  41. id: 'id',
  42. localdata: data
  43. };
  44. var dataAdapter = new $.jqx.dataAdapter(source);
  45. $scope.gridSettings =
  46. {
  47. width: 950,
  48. source: dataAdapter,
  49. columnsresize: true,
  50. columns: [
  51. { text: 'Customer ID', datafield: 'CustomerID', width: 250 },
  52. { text: 'Customer Name', datafield: 'CustomerName', width: 250 },
  53. { text: 'Customer Email', datafield: 'CustomerEmail', width: 250 },
  54. { text: 'Customer ZipCode', datafield: 'CustomerZipCode', width: 250 },
  55. { text: 'Customer Country', datafield: 'CustomerCountry', width: 250 },
  56. { text: 'Customer City', datafield: 'CustomerCity', width: 250 }
  57. ]
  58. };
  59. //now create the widget.
  60. $scope.createWidget = true;
  61. }).error(function (data, status) {
  62. console.log('Something Wrong');
  63. });
  64. });
  65. </script>
  66. }
  67. <h2>AngularJS Grid Binding To JSON</h2>
  68. <div ng-app="myApp" ng-controller="GridCtrl">
  69. <jqx-grid jqx-create="createWidget" jqx-settings="gridSettings"></jqx-grid>
  70. </div>
Output

Output