Simple CRUD Operation In ASP.NET MVC

Introduction

 
In this article, we will learn how to perform a CRUD (Create, Update, Delete) operation using ASP.NET MVC. We will follow the database first approach to create this MVC Project.
 
Prerequisite
 
Please install the latest version of Visual Studio before reading this article.
 

Procedure

 
Let's start our project In ASP.NET MVC. So follow the steps to create new project in Visual Studio.
 
(1) Click On Create New Project
(2) Write ASP.NET in Searchbar
(3) Select ASP.NET Web Application
(4) Give a name to your project (My Project Name: "PracticeProject")
 
 
Now it will show you a new window. Here, you have to select Empty Template and select MVC (checkbox) then click OK.
 
 

Creating Database

 
Next, we need to create a database table where you can store and retrieve the data. I created the table in MS SQL Server Database, as shown below (Note: If you are not aware of how to create a table in MS SQL Server DB, then please learn about that before going ahead in this article. You will easily learn about that within an hour).
 
 
Now we have to connect our database with our MVC Project using the ADO.NET Entity model. Let's follow step-by-step 
 
Right-click on your project then click on Add, then Select the Option ADO.NET Entity model.
 
 
Give a proper name to it.
 
 
Select Option EF Designer from Database and Click Next.
 
 
In the Next window, Click on the "New Connection" option.
 
 
Now you will see the new popup window where you have to select the Data source as "Microsoft SQL server". Write the proper server name and select the database that you want to connect with your MVC project, then click OK.
 
 
Select Next
 
Select Entity Framework 6.x then click Next.
 
 
Now, select the table, drop it down, then check dbo and your table name (which we created in our MS SQL Server DB). Then click Finish.
 
 
Now you can see all the references have been added into your project and the DB table is also connected with the project. Now you need to save the project and rebuild it.
 
 
You now have to a new controller into your controller folder. Right-click on the Controller folder and then add a new controller
 
 
Now you can see multiple options of controllers, but you have to select "MVC 5 Controllers with views using Entity Framework". You will get pre-loaded programs for action methods (add, update, delete, etc) and for its view also. After that, click on "Add".
 
 
Now you have to write the model class name and data context class name properly, or you can select it using the dropdown button. Give a name to your controller ex: cortollernameController.
 
 
If you don't know your context class name, then you can see it from context class file. Just follow the steps.
 
 
Now you have to define the route of your project.
 
 
Now finally you are done with your MVC Project. Now you can run it and perform the CRUD (Create, Read, Update, Delete) Operation
 
Output
 


Similar Articles