Click here to Skip to main content
15,885,985 members
Articles / Web Development / ASP.NET / ASP.NET Core

How to Create a React App with CRUD Operations the Easy Way

Rate me:
Please Sign up or sign in to vote.
4.09/5 (7 votes)
27 Aug 2018CPOL4 min read 18.7K   20   3
In this article, I'll show you how to build a React Single Page App with paging and sorting without writing any code.

Introduction

In this article, I’m going to be building a React App with CRUD (create, read, update, and delete) operations without writing any code whatsoever using Visual Studio 2017 and ASP.NET Core 2.0. To accomplish this, I’ll be using React App Generator, which you can download for free from http://bssdev.biz/React-App-Generator (look for the free download link at the bottom). Note: You will need to create a free account in order to download the app.

Background

React App Generator is an ASP.NET Core tool that allows .NET developers to quickly build a 100% React application. React App Generator creates everything you need for a 100% React application with full CRUD capabilities from either your Database Schema or your Application Models.

Note: This version of React App Generator requires ASP.NET Core 2.0. It is incompatible with ASP.NET Core 1.x and 2.1, which creates React project with different structures.

Let’s start by building our base app…

Building a New React App

  1. Open Visual Studio and select New Project.
  2. From the New Project dialog box, select .NET Core and then ASP.NET Core Web Application (Figure 1):

    new project dialog

    Figure 1.
  3. From the ASP.NET Core Web Application dialog box, select React.js (Figure 2):

    new project dialog 2

    Figure 2.

Creating Our Models

We need to create a model to use with our app. For this article, we’ll be creating a model called Actor as shown below. We’ll name the file models.cs since we’ll be keeping all of our models in this file.

C#
public class Actor
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[Required]
[DisplayName("Id")]
public int Id {get; set;}

[Required]
[MaxLength(50)]
[DisplayName("Name")]
public string Name {get; set;}

[MaxLength(10)]
[DisplayName("Gender")]
public string Gender {get; set;}

[DisplayName("Age")]
public int? Age {get; set;}

[MaxLength(255)]
[DisplayName("Picture")]
public string Picture {get; set;}
}

Note: If you’re new to Visual Studio, you may need to hover over any text with squiggly lines and add any necessary “using” statements.

We’ll also need a DbContext for our model since our data will be coming from a SQL database, but it just as easily could have come from MySQL or a NOSQL DocumentDb store. We’ll name it AppDbContext.cs.

C#
public class AppDbContext : DbContext
 {
 public AppDbContext(DbContextOptions<appdbcontext> options)
 : base(options)
 {
 }
 
 public DbSet<actor> Actors {get; set;}

}
}    
</actor></appdbcontext>

That’s all we need. We can now use React App Generator to create our entire app with CRUD operations, and even built-in paging and sorting…

Running React App Generator

  1. Run the React App Generator application.
  2. In the Source section, specify the path to your models.cs file by clicking the browse button (…) and navigating to it.
  3. In the Table/Model box, specify the name of the model to process using the browse button (…) and selecting it. For this example, it's Actor.
  4. In the DbContext box, specify AppDbContext as the Database Context.
  5. And in the Namespace box, enter the name of the application that you created above in “Building a New React Application”.
  6. Then, click the Let’s Go button to make the magic happen.

If all went well, you should see a Result message box that says everything succeeded and tells you what the next steps are. If you’ve gotten that message, make a mental note of the Next Steps and click the OK button.

To apply the content to the base application we created above, we simply copy the content to our project. To do that, just click the magnifying glass button to the right of the Let’s Go button. Windows Explorer will open showing the output. Simply select all of the files and paste them to the root of your project (the folder that contains the: bin/ and ClientApp/ folders as well as a few others). We just need to configure a few things and we can run our app…

Configuring Your React App

We need to ensure that all of the npm packages required by the app have been downloaded. In Solution Explorer, we need to expand Dependencies, then right-click on the npm folder, and select Restore Packages from the popup menu. It may take a while so you’ll need to be patient.

tsconfig.json

We also need to make sure ‘strict’ mode is turned off in tsconfig.json (located at the root of your application).

C#
"compilerOptions": { "strict": false

AppSettings.json

We also need to make sure we have a connection string for our AppDbContext in appsettings.json. It should look something like this:

C#
"ConnectionStrings": {
 "AppDbContext": "Server=localhost;Database=Movies;Integrated Security=true;"
 },

We can now compile our app by right-clicking on the project in Solution Explorer and selecting Rebuild. If the npm packages have not finished downloading, you’ll get a message indicating the build is being delayed.

And that’s it! Once the app compiles, you can run it…

Image 3

Figure 3.

…you’ll notice we have links for our CRUD operations: Create, Details, Edit and X (Delete), as well as built-in paging and sorting without writing any code (except for creating our model). And React App Generator also includes support for OpenIDConnect authentication and authorization, which I’ll leave for another article. Ciao!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Better Software Solutions, LLC
United States United States
Alex Brambila is a seasoned Software Developer with over 25 years experience with Microsoft technologies ,as well as many others, such as: .NET Core, React, ASP.Net, Visual Studio, MVC, SQL Server, C#, VB, Javascript, Oracle, Android, Java, and many more. He has spent decades working in Information Technology for various government and private organizations. Currently he runs his own business: Better Software Solutions, LLC headquartered in California.

Comments and Discussions

 
QuestionSomething didn't make sense. Pin
KentsCode24-Feb-21 9:49
KentsCode24-Feb-21 9:49 
QuestionTrouble with the example - not compiling </actor></appdbcontext> Pin
DumpsterJuice28-Aug-18 5:04
DumpsterJuice28-Aug-18 5:04 
AnswerRe: Trouble with the example - not compiling </actor></appdbcontext> Pin
stixoffire29-Aug-18 16:23
stixoffire29-Aug-18 16:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.