CRUD Operations In ASP.NET MVC 5 Using ADO.NET
Background
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 i with stepbystep 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:
ActionResult in ASP.NET MVC
Creating an ASP.NET MVC Application
Step 1 : Create an MVC Application.
Now let us start with a stepbystep 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:

Step 2: Create Model Class
Now let us create the model class named EmpModel.cs by right clicking on model folder as in the following screenshot:

Note: It is not mandatory that Model class should be in Model folder, it is just for better readability you can create this class anywhere in the solution explorer. This can be done by creating different folder name or without folder name or in a separate class library.
EmpModel.cs class code snippet:
1 |
public class EmpModel |
In the above model class we have added some validation on properties with the help of DataAnnotations.
Step 3: Create Controller.
Now let us add the MVC 5 controller as in the following screenshot:

After clicking on Add button it will show the following window. Now specify the Controller name as Employee with suffix Controller as in the following screenshot:

Note: The controller name must be having suffix as 'Controller' after specifying the name of controller.
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:

I hope you have created the same table structure as shown above. Now create the stored procedures to insert, update, view and delete the details as in the following code snippet:
- To Insert Records
1 |
Create procedure [dbo].[AddNewEmpDetails] |
- To View Added Records
1 |
Create Procedure [dbo].[GetEmployees] |
- To Update Records
1 |
Create procedure [dbo].[UpdateEmpDetails] |
- To Delete Records
1 |
Create procedure [dbo].[DeleteEmpById] |
Step 5: Create Repository class.
Now create Repository folder and Add EmpRepository.cs class for database related operations, after adding the solution explorer will look like the following screenshot:

Now create methods in EmpRepository.cs to handle the CRUD operation as in the following screenshot:
- EmpRepository.cs
1 |
public class EmpRepository |
Step 6 : Create Methods into the EmployeeController.cs file.
Now open the EmployeeController.cs and create the following action methods:
1 |
public class EmployeeController : Controller |
Step 7: Create Views.
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:
- AddEmployee.cshtml
1 |
@model CRUDUsingMVC.Models.EmpModel @using (Html.BeginForm()) |
- To View Added Employees
To view the employee details let us create the partial view named GetAllEmpDetails:

Now click on add button, it will create GetAllEmpDetails.cshtml strongly typed view whose code is given below:
- GetAllEmpDetails.CsHtml
1 |
@model IEnumerable<CRUDUsingMVC.Models.EmpModel> <p> |
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:
- EditEmpDetails.cshtml
1 |
@model CRUDUsingMVC.Models.EmpModel @using (Html.BeginForm()) |
Step 8 : Configure Action Link to Edit and delete the records as in the following figure:

The above ActionLink I have added in GetAllEmpDetails.CsHtml view because from there we will delete and update the records.
Step 9: Configure RouteConfig.cs to set default action as in the following code snippet:
1 |
public class RouteConfig |
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
Now run the application the AddEmployee view will be appears are as:

Click on save button, the model state validation will fire, as per validation we have set into the EmpModel.cs class:

Now enter the details and on clicking save button, the records get added into the database and the following message appears.

Now click on Back to Employee List hyperlink, it will be redirected to employee details grid as in the following screenshot:

Now similar to above screenshot, add another record, then the list will be as in the following screenshot:

Now click on Edit button of one of the record, then it will be redirected to Edit view as in the following screenshot:

Now click on Update button, on clicking, the records will be updated into the database. Click Back to Details hyperlink then it will be redirected to the Employee list table with updated records as in the following screenshot:

Now click on delete button for one of the records, then the following confirmation box appears (we have set configuration in ActionLink):

Now click on OK button, then the updated Employee list table will be like the following screenshot:

From the preceding examples we have learned how to implement CRUD operations in ASP.NET MVC using ADO.NET.
Note:
- Configure the database connection in the web.config file depending on your database server location.
- Download the Zip file of the sample application for a better understanding .
- 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.
Summary
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 please contact me.
· EOF ·
CRUD Operations In ASP.NET MVC 5 Using ADO.NET的更多相关文章
- [转]Enabling CRUD Operations in ASP.NET Web API 1
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/creating-a-web-api-that ...
- ASP.NET MVC+JQueryEasyUI1.4+ADO.NET Demo
1.JQueryEasyUI使用 JQuery EasyUI中文官网:http://www.jeasyui.net/ JQuery EasyUI中文官网下载地址:http://www.jeasyui. ...
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- [转]ASP.NET MVC 2: Model Validation
本文转自:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx?CommentPo ...
- 【转】ASP.NET MVC教程
转自:http://www.cnblogs.com/QLeelulu/category/123326.html ASP.NET MVC的最佳实践与性能优化的文章 摘要: 就一些文章链接,就不多废话了. ...
- Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展
Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffoldi ...
- 【ASP.NET MVC 5】第27章 Web API与单页应用程序
注:<精通ASP.NET MVC 3框架>受到了出版社和广大读者的充分肯定,这让本人深感欣慰.目前该书的第4版不日即将出版,现在又已开始第5版的翻译,这里先贴出该书的最后一章译稿,仅供大家 ...
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...
- ASP.NET MVC和Web API中的Angular2 - 第2部分
下载源码 内容 第1部分:Visual Studio 2017中的Angular2设置,基本CRUD应用程序,第三方模态弹出控件 第2部分:使用Angular2管道进行过滤/搜索,全局错误处理,调试客 ...
随机推荐
- 使用UEditor无法SetContent的问题
无法SetContent是因为 <script id="txtContent" name="txtContent" type="text/pla ...
- 自制操作系统(二) 让bootsector开机启动打印一首诗
qq:992591601 欢迎交流 2016-03-31作 2016-06-01.2016-06-27改 我总结了些基本原理: 1.软盘的第一个扇区为启动区 2.计算机读软盘是以512字节为单位来读写 ...
- oracle违反完整约束条件
oracle违反完整约束条件 Oracle ORA-02292: 违反完整约束条件 (UNITELE.TA_SUB_REFERENCE3) - 已找到子记录 A表被B表引用,删除A表的时候提示ORA- ...
- php 3种常见设计模式
1.工厂模式 <?php namespace Facebab; class Factory { static function createDatabase () { return new Da ...
- js 停止事件冒泡 阻止浏览器的默认行为(阻止超连接 # )
在前端开发工作中,由于浏览器兼容性等问题,我们会经常用到“停止事件冒泡”和“阻止浏览器默认行为”. 1..停止事件冒泡 JavaScript代码 //如果提供了事件对象,则这是一个非IE浏览器if ( ...
- 《STL系列》之vector原理及实现
最近忙得蛋疼,但还是想写点属于自己的东西.也不知道写点啥,最后决定试着自己实现STL中常用的几个集合,一来加深自己对STL的理解,二来看看自己是否有这个能力实现.实现目标就是:1能和STL兼容:2最大 ...
- 关于ScrollerView的一些小心得
在项目开发时遇到一个问题,我在UIViewController上面直接创建了一个UIScrollerView,把UIScrollerView作为一个子视图添加到了UIViewController, 又 ...
- easyui datagrid去掉加载提示
掉这个等待效果的方法:查了easyui的api,datagrid没有去掉这个遮罩层的方法或者属性,在网上找了半天也没人碰到相同的问题(可能比较easy就解决了吧).还好easyui是开源的,就研究它的 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Swift编程语言SequenceType协议中的一些比较有用的接口
在Swift编程语言中,大部分容器类(比如Array.Dictionary)都实现了SequenceType协议.SequenceType协议中有不少有趣且简便的方法可用来实现我们不少实际需求.这里将 ...