In this article I will explain with an example, how to connect ReactJS with SQL Server database in ASP.Net Core MVC.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
The Model class consists of following properties.
public class Customer
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
public class DBCtx : DbContext
    public DBCtx(DbContextOptions<DBCtx> options) : base(options)
    public DbSet<Customer> Customers { get; set; }
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling XmlHttpRequest (XHR) AJAX operation
This Action method handles the AJAX call made from the XmlHttpRequest (XHR) function inside the ReactJS class.
Note: The following Action method handles POST call and will return JSON object and hence the return type is set to JsonResult.
This Action method returns Top 10 records from the Customers table as JSON to the XmlHttpRequest (XHR) function inside the ReactJS class.
public class HomeController : Controller
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
        this.Context = _context;
    public IActionResult Index()
        return View();
    public JsonResult AjaxMethod()
        List<Customer> customers = (from customer in this.Context.Customers.Take(10)
                                    select customer).ToList();
        return Json(customers);
ReactJS implementation
Following ReactJS.jsx file consists of a CustomerTable class which extends ReactJS Component.
Inside the Constructor, the props variable is sent to the super class so that it can be used later in the render function.
Then an Array named customers is assigned to the state.
Event Handlers
This function is called when the React Component is loaded in the DOM.
Inside this function, an XmlHttpRequest (XHR) AJAX request is made to the Controller’s Action method.
And inside the onload event handler, the received response is set in the customers Array of the state using the setState function.
Render function
The Render function returns an HTML Table with Header Row and dynamic rows generated using the customers Array.
Inside the Render function, with the help of map function, the Table rows are dynamically generated using the customers Array.
Finally, the ReactDOM.render function, render’s the class in a HTML DIV with ID dvCustomersGrid.
class CustomerTable extends React.Component {
    constructor(props) {
        this.state = {
            customers: []
    componentDidMount() {
        var request;
        if (window.XMLHttpRequest) {
            //New browsers.
            request = new XMLHttpRequest();
        else if (window.ActiveXObject) {
            //Old IE Browsers.
            request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request != null) {
            request.onload = function () {
                if (request.readyState == 4 && request.status == 200) {
                    var response = JSON.parse(request.responseText);
                    this.setState({ customers: response });
    render() {
        return (<table cellPadding="0" cellSpacing="0">
                { =>
ReactDOM.render(<CustomerTable />,
The View consists of an HTML DIV dvCustomersGrid inside which the message from the ReactJS will be displayed.
Then the following necessary JS files for ReactJS application are inherited.
1. react.development.js
2. react-dom.development.js
Finally, the Customers.jsx file is inherited.
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <div id="dvCustomersGrid"></div>
    <script src=""></script>
    <script src=""></script>
    <script src="@Url.Content("~/Scripts/Customers.jsx")"></script>
ASP.Net Core: Connect ReactJS with SQL Server Database