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
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
- {
-
- 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.