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
  1. USE [master]
  2. GO
  3. /****** Object: Database [DbEmployee] Script Date: 3/26/2017 6:19:33 AM ******/
  4. CREATE DATABASE [DbEmployee]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. ( NAME = N'DbEmployee', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DbEmployee.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOG ON
  9. ( 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%)
  10. GO
  11. ALTER DATABASE [DbEmployee] SET COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  14. begin
  15. EXEC [DbEmployee].[dbo].[sp_fulltext_database] @action = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE [DbEmployee] SET ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE [DbEmployee] SET ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE [DbEmployee] SET ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE [DbEmployee] SET ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE [DbEmployee] SET ARITHABORT OFF
  27. GO
  28. ALTER DATABASE [DbEmployee] SET AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE [DbEmployee] SET AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [DbEmployee] SET AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE [DbEmployee] SET CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE [DbEmployee] SET CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE [DbEmployee] SET CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE [DbEmployee] SET NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE [DbEmployee] SET QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE [DbEmployee] SET RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE [DbEmployee] SET DISABLE_BROKER
  49. GO
  50. ALTER DATABASE [DbEmployee] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE [DbEmployee] SET DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE [DbEmployee] SET TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE [DbEmployee] SET ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE [DbEmployee] SET PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE [DbEmployee] SET READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE [DbEmployee] SET HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE [DbEmployee] SET RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE [DbEmployee] SET MULTI_USER
  67. GO
  68. ALTER DATABASE [DbEmployee] SET PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE [DbEmployee] SET DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE [DbEmployee] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  73. GO
  74. ALTER DATABASE [DbEmployee] SET TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE [DbEmployee] SET READ_WRITE
  77. GO
Create Table
  1. USE [DbEmployee]
  2. GO
  3. /****** Object: Table [dbo].[EmployeeTable] Script Date: 3/26/2017 6:20:03 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].[EmployeeTable](
  11. [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
  12. [FirstName] [varchar](50) NULL,
  13. [LastName] [varchar](50) NULL,
  14. [Gender] [varchar](50) NULL,
  15. [Designation] [nchar](10) NULL,
  16. [Salary] [int] NULL,
  17. [City] [varchar](50) NULL,
  18. [Country] [varchar](50) NULL,
  19. CONSTRAINT [PK_EmployeeTable] PRIMARY KEY CLUSTERED
  20. (
  21. [EmployeeID] ASC
  22. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  23. ) ON [PRIMARY]
  24. GO
  25. SET ANSI_PADDING OFF
  26. GO
After creating the table, you can add some records as shown below.
React
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.
ASP.NET Web Application
The next step is to select a template. In this example, we need to choose the Web API template and click the Ok button.
Web API template
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.
ADO.NET Entity Data Model
Now, we are going to choose EF Designer from the database as shown below.
Entity Data Model Wizard
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.
Entity Data Model Wizard
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.
EDMX model
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.
Web API 2 Controller
Enter Controller name (‘EmployeeController’).
Web API 2 Controller
EmployeeController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. namespace DisplayDataReactJS.Controllers
  8. {
  9. [RoutePrefix("api/Employee")]
  10. public class EmployeeController : ApiController
  11. {
  12. //Db Context
  13. DbEmployeeEntities db = new DbEmployeeEntities();
  14. [Route("GetEmployeeList")]
  15. public IQueryable<EmployeeTable> GetEmployeeList()
  16. {
  17. return db.EmployeeTables.AsQueryable();
  18. }
  19. }
  20. }
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
install-package react.js
PM> install-package React.Web.Mvc4
install-package React.Web.Mvc4
In order to create new jsx file. Right click on Scripts folder > Add > JavaScript File.
create jsx file
EmployeeJSX.jsx
  1. var EmployeeRow = React.createClass({
  2. render: function () {
  3. return(
  4. <tr>
  5. <td>{this.props.item.EmployeeID}</td>
  6. <td>{this.props.item.FirstName}</td>
  7. <td>{this.props.item.LastName}</td>
  8. <td>{this.props.item.Gender}</td>
  9. <td>{this.props.item.Designation}</td>
  10. <td>{this.props.item.Salary}</td>
  11. <td>{this.props.item.City}</td>
  12. <td>{this.props.item.Country}</td>
  13. </tr>
  14. );
  15. }
  16. });
  17. var EmployeeTable = React.createClass({
  18. getInitialState: function(){
  19. return{
  20. result:[]
  21. }
  22. },
  23. componentWillMount: function(){
  24. var xhr = new XMLHttpRequest();
  25. xhr.open('get', this.props.url, true);
  26. xhr.onload = function () {
  27. var response = JSON.parse(xhr.responseText);
  28. this.setState({ result: response });
  29. }.bind(this);
  30. xhr.send();
  31. },
  32. render: function(){
  33. var rows = [];
  34. this.state.result.forEach(function (item) {
  35. rows.push(<EmployeeRow key={item.EmployeeID} item={item}/>);
  36. });
  37. return (<table className="table">
  38. <thead>
  39. <tr>
  40. <th>EmployeeID</th>
  41. <th>FirstName</th>
  42. <th>LastName</th>
  43. <th>Gender</th>
  44. <th>Designation</th>
  45. <th>Salary</th>
  46. <th>City</th>
  47. <th>Country</th>
  48. </tr>
  49. </thead>
  50. <tbody>
  51. {rows}
  52. </tbody>
  53. </table>);
  54. }
  55. });
  56. ReactDOM.render(<EmployeeTable url="api/Employee/GetEmployeeList"/>,
  57. document.getElementById('grid'))
Create HTML Page
To add HTML Page. Right click on project name > Add > HTML Page.
React
EmployeeGrid.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>.: Employee Grid :.</title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <h3>Employee List - Web API 2 & ReactJS </h3>
  9. <div id="grid" class="container">
  10. </div>
  11. <!--CSS-->
  12. <link href="Content/bootstrap.min.css" rel="stylesheet" />
  13. <!-- JS -->
  14. <script src="Scripts/jquery-1.10.2.min.js"></script>
  15. <script src="Scripts/react/react.js"></script>
  16. <script src="Scripts/react/react-dom.js"></script>
  17. <script src="Scripts/EmployeeJSX.jsx"></script>
  18. </body>
  19. </html>
Note
Don’t forget to add the following libraries in your HTML page.
  1. <!--CSS-->
  2. <link href="Content/bootstrap.min.css" rel="stylesheet" />
  3. <!-- JS -->
  4. <script src="Scripts/jquery-1.10.2.min.js"></script>
  5. <script src="Scripts/react/react.js"></script>
  6. <script src="Scripts/react/react-dom.js"></script>
  7. <script src="Scripts/EmployeeJSX.jsx"></script>
Output
Now build your application and you can see the following output.
Web API 2 ReactJS
Happy Coding.