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.
Create Table - USE [CustomerDB]
- GO
-
- /****** Object: Table [dbo].[Customers] Script Date: 9/11/2016 4:53:43 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Customers](
- [CustomerID] [int] NOT NULL,
- [CustomerName] [varchar](50) NULL,
- [CustomerEmail] [varchar](50) NULL,
- [CustomerZipCode] [int] NULL,
- [CustomerCountry] [varchar](50) NULL,
- [CustomerCity] [varchar](50) NULL,
- CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
- (
- [CustomerID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- GO
After creating the table, you can add some records, as shown below.
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.
Next, a new dialog will pop up for selecting the template. We are going choose MVC template and click OK.
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.
At this level, we are going to choose EF Designer from database, as show below.
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.
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.
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.
Enter Controller name (‘CustomersController’).
CustomersController.cs - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace AngularJS_Grid_Binding_To_JSON.Controllers
- {
- public class CustomersController : Controller
- {
-
- private CustomerDBEntities context = new CustomerDBEntities();
-
-
- public ActionResult Index()
- {
- return View();
- }
-
-
- public JsonResult GetCustomers()
- {
- var CustomersList = context.Customers.ToList();
-
- return Json(CustomersList, JsonRequestBehavior.AllowGet);
- }
-
-
- }
- }
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.
Note: Don’t forget to download the following libraries from jqxwidgets.
- <!-- CSS -->
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
- <!-- JS -->
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxbuttons.js"></script>
- <script src="~/Scripts/jqxcheckbox.js"></script>
- <script src="~/Scripts/jqxgrid.js"></script>
- <script src="~/Scripts/jqxgrid.selection.js"></script>
- <script src="~/Scripts/jqxmenu.js"></script>
- <script src="~/Scripts/jqxscrollbar.js"></script>
- <script src="~/Scripts/jqxgrid.sort.js"></script>
- <script src="~/Scripts/jqxgrid.columnsresize.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/demos.js"></script>
Index cshtml - @{
- ViewBag.Title = "Index";
- }
-
-
-
- @section scripts{
-
- <!-- CSS -->
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
- <!-- JS -->
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxbuttons.js"></script>
- <script src="~/Scripts/jqxcheckbox.js"></script>
- <script src="~/Scripts/jqxgrid.js"></script>
- <script src="~/Scripts/jqxgrid.selection.js"></script>
- <script src="~/Scripts/jqxmenu.js"></script>
- <script src="~/Scripts/jqxscrollbar.js"></script>
- <script src="~/Scripts/jqxgrid.sort.js"></script>
- <script src="~/Scripts/jqxgrid.columnsresize.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/demos.js"></script>
-
- <script type="text/javascript">
-
- var app = angular.module('myApp', ['jqwidgets']);
- app.controller('GridCtrl', function ($scope, $http) {
-
- $scope.createWidget = false;
- $http({
-
- method: 'GET',
- url: 'GetCustomers'
- }).success(function (data, status) {
-
-
- var source = {
-
- datatype: "json",
- datafields: [
- { name: 'CustomerID', type: 'int' },
- { name: 'CustomerName', type: 'string' },
- { name: 'CustomerEmail', type: 'string' },
- { name: 'CustomerZipCode', type: 'int' },
- { name: 'CustomerCountry', type: 'string' },
- { name: 'CustomerCity', type: 'string' }
- ],
- id: 'id',
- localdata: data
- };
-
- var dataAdapter = new $.jqx.dataAdapter(source);
- $scope.gridSettings =
- {
- width: 950,
- source: dataAdapter,
- columnsresize: true,
-
- columns: [
- { text: 'Customer ID', datafield: 'CustomerID', width: 250 },
- { text: 'Customer Name', datafield: 'CustomerName', width: 250 },
- { text: 'Customer Email', datafield: 'CustomerEmail', width: 250 },
- { text: 'Customer ZipCode', datafield: 'CustomerZipCode', width: 250 },
- { text: 'Customer Country', datafield: 'CustomerCountry', width: 250 },
- { text: 'Customer City', datafield: 'CustomerCity', width: 250 }
-
- ]
-
- };
-
- $scope.createWidget = true;
-
- }).error(function (data, status) {
-
- console.log('Something Wrong');
- });
-
- });
-
-
- </script>
-
- }
-
- <h2>AngularJS Grid Binding To JSON</h2>
-
- <div ng-app="myApp" ng-controller="GridCtrl">
- <jqx-grid jqx-create="createWidget" jqx-settings="gridSettings"></jqx-grid>
- </div>
Output