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
- USE[master]
- GO
- /****** Object: Database [CustomerDB] Script Date: 9/11/2016 4:54:28 AM ******/
- CREATE DATABASE[CustomerDB]
- CONTAINMENT = NONE
- ON PRIMARY
- (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)
- LOG ON
- (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 % )
- GO
- ALTER DATABASE[CustomerDB] SET COMPATIBILITY_LEVEL = 110
- GO
- IF(1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC[CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE[CustomerDB] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE[CustomerDB] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE[CustomerDB] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE[CustomerDB] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE[CustomerDB] SET ARITHABORT OFF
- GO
- ALTER DATABASE[CustomerDB] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE[CustomerDB] SET AUTO_CREATE_STATISTICS ON
- GO
- ALTER DATABASE[CustomerDB] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE[CustomerDB] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE[CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE[CustomerDB] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE[CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE[CustomerDB] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE[CustomerDB] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE[CustomerDB] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE[CustomerDB] SET DISABLE_BROKER
- GO
- ALTER DATABASE[CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE[CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE[CustomerDB] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE[CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE[CustomerDB] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE[CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE[CustomerDB] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE[CustomerDB] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE[CustomerDB] SET MULTI_USER
- GO
- ALTER DATABASE[CustomerDB] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE[CustomerDB] SET DB_CHAINING OFF
- GO
- ALTER DATABASE[CustomerDB] SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF)
- GO
- ALTER DATABASE[CustomerDB] SET TARGET_RECOVERY_TIME = 0 SECONDS
- GO
- ALTER DATABASE[CustomerDB] SET READ_WRITE
- GO
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.
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.
The next step is to select template. In this example, we need to choose MVC template and click OK.
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.
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.
Enter Controller name (‘CustomersController’).
CustomersController.cs
- 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);
- }
- }
- }
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.
Note
Don’t forget to download the following libraries from jqxwidgets.
-
- <
- link href = "~/Content/jqx.base.css"
- rel = "stylesheet" / >
-
- <
- script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js" > < /script> <
- script src = "~/Scripts/jqxcore.js" > < /script> <
- script src = "~/Scripts/jqxangular.js" > < /script> <
- script src = "~/Scripts/jqxbuttons.js" > < /script> <
- script src = "~/Scripts/jqxscrollbar.js" > < /script> <
- script src = "~/Scripts/jqxdata.js" > < /script> <
- script src = "~/Scripts/jqxdatatable.js" > < /script> <
- script src = "~/Scripts/demos.js" > < /script>
Index cshtml - @ {
- ViewBag.Title = "CustomerView";
- }
- @section scripts {
-
- <
- link href = "~/Content/jqx.base.css"
- rel = "stylesheet" / >
-
- <
- script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js" > < /script> <
- script src = "~/Scripts/jqxcore.js" > < /script> <
- script src = "~/Scripts/jqxangular.js" > < /script> <
- script src = "~/Scripts/jqxbuttons.js" > < /script> <
- script src = "~/Scripts/jqxscrollbar.js" > < /script> <
- script src = "~/Scripts/jqxdata.js" > < /script> <
- script src = "~/Scripts/jqxdatatable.js" > < /script> <
- script src = "~/Scripts/demos.js" > < /script> <
- script type = "text/javascript" >
- var myApp = angular.module('myApp', ['jqwidgets']);
- myApp.controller('DataTableCtrl', ['$scope', function($scope) {
- $scope.gridSettings = {
- width: 850,
- pageable: true,
- pagerButtonsCount: 10,
- source: new $.jqx.dataAdapter({
- dataType: "json",
- dataFields: [{
- name: 'CustomerName',
- type: 'string'
- }, {
- name: 'CustomerEmail',
- type: 'string'
- }, {
- name: 'CustomerZipCode',
- type: 'int'
- }, {
- name: 'CustomerCountry',
- type: 'string'
- }, {
- name: 'CustomerCity',
- type: 'string'
- }],
- id: 'CustomerID',
- url: 'GetCustomerList'
- }),
- columnsResize: true,
- columns: [{
- text: 'CustomerName',
- dataField: 'CustomerName',
- width: 300
- }, {
- text: 'CustomerEmail',
- dataField: 'CustomerEmail',
- width: 300
- }, {
- text: 'CustomerZipCode',
- dataField: 'CustomerZipCode',
- width: 300
- }, {
- text: 'CustomerCountry',
- dataField: 'CustomerCountry',
- width: 300
- }, {
- text: 'CustomerCity',
- dataField: 'CustomerCity',
- width: 300
- }]
- };
- }]); <
- /script>
- } <
- div ng - app = "myApp"
- ng - controller = "DataTableCtrl" >
- <
- h2 > Data Binding to JSON data in AngularJS DataTable < /h2> <
- jqx - data - table jqx - settings = "gridSettings" > < /jqx-data-table> <
- /div>
Output