Introduction
In this post, we will see how to bind data in a grid by using the ReactJS library, ASP.Net Web API 2, and Entity Framework ORM.
In this demo, we are going create a sample application, then generate a mapping of our employee table by using Entity Framework, after that, we will create a service which returns all data from employee table. Finally, ReactJS as a library will be used in order to bind data in a grid.
Let’s start.
SQL Database part
Here, you will find the scripts to create a database and table.
Create Database
- USE [master]
- GO
- /****** Object: Database [DbEmployee] Script Date: 3/26/2017 6:19:33 AM ******/
- CREATE DATABASE [DbEmployee]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'DbEmployee', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbEmployee.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
- LOG ON
- ( NAME = N'DbEmployee_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbEmployee_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
- GO
- ALTER DATABASE [DbEmployee] SET COMPATIBILITY_LEVEL = 110
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [DbEmployee].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [DbEmployee] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [DbEmployee] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [DbEmployee] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [DbEmployee] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [DbEmployee] SET ARITHABORT OFF
- GO
- ALTER DATABASE [DbEmployee] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [DbEmployee] SET AUTO_CREATE_STATISTICS ON
- GO
- ALTER DATABASE [DbEmployee] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [DbEmployee] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [DbEmployee] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [DbEmployee] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [DbEmployee] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [DbEmployee] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [DbEmployee] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [DbEmployee] SET DISABLE_BROKER
- GO
- ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [DbEmployee] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [DbEmployee] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [DbEmployee] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [DbEmployee] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [DbEmployee] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [DbEmployee] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [DbEmployee] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE [DbEmployee] SET MULTI_USER
- GO
- ALTER DATABASE [DbEmployee] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [DbEmployee] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [DbEmployee] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
- GO
- ALTER DATABASE [DbEmployee] SET TARGET_RECOVERY_TIME = 0 SECONDS
- GO
- ALTER DATABASE [DbEmployee] SET READ_WRITE
- GO
Create Table
- USE [DbEmployee]
- GO
- /****** Object: Table [dbo].[EmployeeTable] Script Date: 3/26/2017 6:20:03 AM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[EmployeeTable](
- [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
- [FirstName] [varchar](50) NULL,
- [LastName] [varchar](50) NULL,
- [Gender] [varchar](50) NULL,
- [Designation] [nchar](10) NULL,
- [Salary] [int] NULL,
- [City] [varchar](50) NULL,
- [Country] [varchar](50) NULL,
- CONSTRAINT [PK_EmployeeTable] PRIMARY KEY CLUSTERED
- (
- [EmployeeID] 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 Web API application
Open Visual Studio and select File >> New Project.
The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

The next step is to select a template. In this example, we need to choose the Web API template and click the Ok button.

After creating our 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 dialog box will pop up, inside Visual C# select Data then ADO.NET Entity Data Model, and enter a name for your Dbcontext model as DbEmployee, finally click Add.

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

After clicking the 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 the database via dropdown List (DB Employee), then click OK button.

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

Create a controller
Now, we are going to create a controller. Right-click on the controllers folder > Add > Controller> selecting Web API 2 Controller – Empty > click Add.

Enter Controller name (‘EmployeeController’).

EmployeeController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace DisplayDataReactJS.Controllers
- {
- [RoutePrefix("api/Employee")]
- public class EmployeeController : ApiController
- {
- //Db Context
- DbEmployeeEntities db = new DbEmployeeEntities();
- [Route("GetEmployeeList")]
- public IQueryable<EmployeeTable> GetEmployeeList()
- {
- return db.EmployeeTables.AsQueryable();
- }
- }
- }
As you can see, I am creating GetEmployeeList() action to select all data from the employee table in JSON format.
ReactJS part
Before all this, we are installing the ReactJS library. For this, open package manager console and running the following commands:
PM> install-package react.js

PM> install-package React.Web.Mvc4

In order to create new jsx file. Right click on Scripts folder > Add > JavaScript File.

EmployeeJSX.jsx
- var EmployeeRow = React.createClass({
- render: function () {
- return(
- <tr>
- <td>{this.props.item.EmployeeID}</td>
- <td>{this.props.item.FirstName}</td>
- <td>{this.props.item.LastName}</td>
- <td>{this.props.item.Gender}</td>
- <td>{this.props.item.Designation}</td>
- <td>{this.props.item.Salary}</td>
- <td>{this.props.item.City}</td>
- <td>{this.props.item.Country}</td>
- </tr>
- );
- }
- });
- var EmployeeTable = React.createClass({
- getInitialState: function(){
- return{
- result:[]
- }
- },
- componentWillMount: function(){
- var xhr = new XMLHttpRequest();
- xhr.open('get', this.props.url, true);
- xhr.onload = function () {
- var response = JSON.parse(xhr.responseText);
- this.setState({ result: response });
- }.bind(this);
- xhr.send();
- },
- render: function(){
- var rows = [];
- this.state.result.forEach(function (item) {
- rows.push(<EmployeeRow key={item.EmployeeID} item={item}/>);
- });
- return (<table className="table">
- <thead>
- <tr>
- <th>EmployeeID</th>
- <th>FirstName</th>
- <th>LastName</th>
- <th>Gender</th>
- <th>Designation</th>
- <th>Salary</th>
- <th>City</th>
- <th>Country</th>
- </tr>
- </thead>
- <tbody>
- {rows}
- </tbody>
- </table>);
- }
- });
- ReactDOM.render(<EmployeeTable url="api/Employee/GetEmployeeList"/>,
- document.getElementById('grid'))
Create HTML Page
To add HTML Page. Right click on project name > Add > HTML Page.

EmployeeGrid.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>.: Employee Grid :.</title>
- <meta charset="utf-8" />
- </head>
- <body>
- <h3>Employee List - Web API 2 & ReactJS </h3>
- <div id="grid" class="container">
- </div>
- <!--CSS-->
- <link href="Content/bootstrap.min.css" rel="stylesheet" />
- <!-- JS -->
- <script src="Scripts/jquery-1.10.2.min.js"></script>
- <script src="Scripts/react/react.js"></script>
- <script src="Scripts/react/react-dom.js"></script>
- <script src="Scripts/EmployeeJSX.jsx"></script>
- </body>
- </html>
Note
Don’t forget to add the following libraries in your HTML page.
- <!--CSS-->
- <link href="Content/bootstrap.min.css" rel="stylesheet" />
- <!-- JS -->
- <script src="Scripts/jquery-1.10.2.min.js"></script>
- <script src="Scripts/react/react.js"></script>
- <script src="Scripts/react/react-dom.js"></script>
- <script src="Scripts/EmployeeJSX.jsx"></script>
Output
Now build your application and you can see the following output.
Happy Coding.

Guest UserPosted Feb 10, 2019, 10:12 PM
Thanks for the article. I couldn't get this to work. However, I'm troubleshooting the JSX.
Les PinterPosted Jul 22, 2018, 5:05 PM
I really appreciate your article. First time I've displayed a table of SQL data using React. I have a question, though, and perhaps you can point me in the right direction. I want to pass a parameter - in my case the first letter of the last name - to the query, preferably based on a dropdown listbox in the html page. I've already noted that if I change the ReactDOM.Render url to end in "?letter=A" the modified query works. What modifications would I need to make to do that interactively? Thanks in advance for your suggestions.
SUJIL KUMARPosted Apr 18, 2018, 2:27 AM
Great Article , Please can you share the Code with Complete CRUD operation with Same Sample ?
kzelda linkPosted Apr 16, 2017, 4:47 AM
Good Article , thx