After awesome response of an published by me in the year 2013: Insert, Update, Delete In GridView Using ASP.Net C#. It now has more than 140 K views, therefore to help beginners I decided to rewrite the article with step by step approach using ASP.NET MVC, since it is a hot topic in the market today. I have written this article focusing on beginners so they can understand the basics of MVC. Please read my previous article using the following links to understand the basics about MVC:
- CRUD Operations In ASP.NET MVC 5 Using Dapper
- ActionResult in ASP.NET MVC.
- Creating an ASP.NET MVC Application.
- Convert DataTable To Generic List In ASP.NET MVC .
- Show Confirm Alert Box on ActionLink Click In ASP.NET MVC.
- Convert DataTable To Generic List Using LINQ In ASP.NET MVC.
- How to Change ActionLink Text Color In ASP.NET MVC .
- How to Call Another Controller View Using ActionLink In ASP.NET MVC .
- Convert DataTable To Generic List Using AsEnumerable In ASP.NET MVC .
Now let us start with a step by step approach from the creation of simple MVC application as in the following:
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project..." then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK. After clicking, the following window will appear:
- As shown in the preceding screenshot, click on Empty template and check MVC option, then click OK. This will create an empty MVC web application whose Solution Explorer will look like the following:
EmpModel.cs class code snippet:
Step 3: Create Controller.
Now let us add the MVC 5 controller as in the following screenshot:
After clicking on Add button controller is created with by default code that support CRUD operations and later on we can configure it as per our requirements.
Step 4 : Create Table and Stored procedures.
Now before creating the views let us create the table name Employee in database according to our model fields to store the details:
To Insert Records
Now create Repository folder and Add EmpRepository.cs class for database related operations, after adding the solution explorer will look like the following screenshot:
Step 6 : Create Methods into the EmployeeController.cs file.
Now open the EmployeeController.cs and create the following action methods:
Create the Partial view to Add the employees
To create the Partial View to add Employees, right click on ActionResult method and then click Add view. Now specify the view name, template name and model class in EmpModel.cs and click on Add button as in the following screenshot:
After clicking on Add button it generates the strongly typed view whose code is given below:
Now click on add button, it will create GetAllEmpDetails.cshtml strongly typed view whose code is given below:
To Update Added Employees
Follow the same procedure and create EditEmpDetails view to edit the employees. After creating the view the code will be like the following:
From the above RouteConfig.cs the default action method we have set is AddEmployee. It means that after running the application the AddEmployee view will be executed first.
Now after adding the all model, views and controller our solution explorer will be look like as in the following screenshot:
Step 10: Run the application and then the output will be like as following demo
- Configure the database connection in the web.config file depending on your database server location.
- Since this is a demo, it might not be using proper standards, so improve it depending on your skills
- This application is created completely focusing on beginners.
In my next article explains the types of controllers in MVC. I hope this article is useful for all readers. If you have any suggestion then use the comment box to write your suggestion.