In this tutorial, I am going to show you how to add CRUD (Create, Read, Update, Detail) functionality in ASP.NET Core 2.0 application. CRUD operations are the basics of every application. So, we will implement CRUD operations step by step in our project.
Previous Tutorials of ASP.NET Core 2.0 series.
- Introduction to ASP.NET Core 2.0
- How to create a new project in ASP.NET Core 2.0 using Visual Studio.
- Project Folder structure and responsibility of each file in ASP NET Core 2.0 Application.
- Middleware in ASP.NET Core.
- Routing in ASP.NET Core.
- Attribute-based Routing in ASP.NET Core.
- Tag Helpers in ASP NET Core 2.0
- Action Results in ASP.NET Core 2.0
- Model and Entity Framework Core in ASP.NET Core 2.0
What will you learn in this tutorial?
As you know, this tutorial is a part of the previous tutorial. Actually, we are creating a small “StudentRecord” project in ASP.NET Core 2.0 Application. So, we will learn the below points in this tutorial.
- Index Action method and View for Index action method.
- Create Action method and View for Create action method.
- Edit Action method and View for Edit action method.
- Details action method and view for detail action method.
- Delete action method and view for delete action method.
Let’s start in step by step.
Note: – I will not create a new project in this tutorial, i will use that project which I have created in one of the previous tutorials. And one more thing, I will not create a new database, because I have already created it in the previous tutorial, So, if you want to know how to add model and create a new database, then click here.
Step 1: Add a new Controller
First-of-all, in this step, add a new controller in the project.
Go to solution explorer => right click on the controller folder => choose Add => then choose controller. It will show a popup.
Then select MVC Controller – Empty => then click Add button.
Then enter the name of the controller like “StudentController” => then click ok. It will add a new file within the “Controllers” folder with the name “StudentController.cs”. You will see one index action method within the newly created controller class.
Step 2: Add Database object.
After creating the controller, we need to add database object to the controller to make changes to the database. So, copy the below code lines and paste into the StudentController.cs file.
Note: – So, we will use _db to access the database.
Step 3: Add Index Action
Now, replace the below code with the index action in StudentController class. This action will retrieve all the data into a list and then provide it to the index view.
Step 4: Add Index View
So, we have created the index action method successfully, now its time to add a view for index action method.
Just right-click on the Index action method => Choose “Add View”. It will open a popup. Now, there is no need to change anything. Just click Add button. Then you will see it will add a view to student views folder (which is under the Views folder).
Now, copy the below code and replace it with the newly created index view file.
Line # 1: this line will tie this view to student model. And “IEnumerable” means it will get a list of record not a single record.
Step 5: Add Create Action Method.
Now, in this section, we will add a new action method for creating the record. Just copy the below code the paste into the Controller class just after the index action method.
Line # 1 to 4: This is HttpGet method.
Line # 5 to 16: This is HttpPost Method.
Line # 9: It will check the model state.
Line # 11: It will add student record into the database.
Line # 15: Simply return the Index View.
Step 6: Add Create View.
Just right click on the Create (HttpPost) action method => Choose “Add View”. It will open a popup. Now, there is no need to change anything. Just click Add button. Then you will see it will add a view under student views folder with the name of “Create”.
Now copy the below code and paste into the Create view file.
Step 7: Add Details Action Method.
Now, in this section, we will add a new action method for to view single student’s record. Just copy the below code the paste into the Controller class just after the Create action method.
Step 8: Add View for Details Action Method.
Just right click on the Detail action method => Choose “Add View”. It will open a popup. Now, these is no need to change anything. Just click Add button. Then you will see it will add a view under student views folder with the name of “Details”.
Now copy the below code and paste into the Create view file.
Step 9: Add Edit Action Method.
Now, in this section, we will add a new action method to edit student’s record. Just copy the below code the paste into the Controller class just after the Details action method.
Step 10: Add View for Edit Action Method.
Just right click on the Edit action method (HttpPost) => Choose “Add View”. It will open a popup. Now, there is no need to change anything. Just click Add button. Then you will see it will add a view under student views folder with the name of “Edit”.
Now copy the below code and paste into the Create view file.
Step 11: Add Delete Action method.
Now, in this section, we will add a new action method for deleting the student record. Just copy the below code the paste into the Controller class just after the Edit action method.
Step 12: Add View for Delete Action Method.
Just right click on the Delete action method => Choose “Add View”. It will open a popup. Now, these is no need to change anything. Just click Add button. Then you will see it will add a view under student views folder with the name of “Delete”.
Now copy the below code and paste into the Create view file.
Step 13: Some Changes in _Layout.cshtml file.
In this section, we will add some changes in the _Layout.cshtml file. Let’s see what changes we do in that file.
Go to solution explorer => open Views folder => open Shared folder => open the “_Layout.cshtml” file.
Line # 29: Changes in Project Name.
Line # 34: Changes in the menu items. I have removed the “About” and “Contact” menu items. And I added a new item into the menu with the name of “Students”
Leave a Reply