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管道进行过滤/搜索,全局错误处理,调试客 ...
随机推荐
- [ucgui] 彩色条函数
/* 颜色条 */ void ShowColorBar(void) { , y0 = , yStep = , i; int NumColors = LCD_GetDevCap(LCD_DEVCAP_N ...
- C# 通过SerialPort简单调用串口
问题 最近比较经常使用串口进行发送以及传输数据,但是笔者在刚开始接触SerialPort类时,对于Write之后去Read数据的时候,由于设备上面还没有返回数据,读取到的只能是空值.然而,再进行下一次 ...
- 关于堆排序和topK算法的PHP实现
问题描述 topK算法,简而言之,就是求n个数据里的前m大个数据,一般而言,m<<n,也就是说,n可能有几千万,而m只是10或者20这样的两位数. 思路 最简单的思路,当然是使用要先对这n ...
- C语言实现二叉树-04版
二叉树,通常应当是研究其他一些复杂的数据结构的基础.因此,通常我们应该精通它,而不是了解:当然,可能并不是每个人都认同这种观点,甚至有些人认为理解数据结构就行了!根本没有必要去研究如何实现,因为大多数 ...
- java利用16进制来辨别png格式的图片
很多人知道利用.png的字符串结尾可以判断前端传入的图片是否为png格式,但是这只是潜意识的判断!那么如何利用png读写的特殊内容来深意识地判断图片格式呢?最近在做东西的时候遇到了点问题,在加载图片的 ...
- 修改Oracle并行度的方法
Oracle并行度默认为1,适当修改并行度对提高性能有很大帮助 1.查看并行度 select table_name,degree from user_tables; --并行度按照用户表分别设置 2. ...
- Leetcode 155 Min Stack
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同 ...
- Liferay7 BPM门户开发之32: 实现自定义认证登陆(定制Authentication Hook)
第一步:修改liferay-hook.xml <?xml version="1.0"?> <!DOCTYPE hook PUBLIC "-//Lifer ...
- NGUI ScrollView 循环 Item 实现性能优化
今天来说说一直都让我在项目中头疼的其中一个问题,NGUI 的scrollView 列表性能问题,实现循环使用item减少性能上的开销. 希望能够给其他同学们使用和提供一个我个人的思路,这个写的不是太完 ...
- Enterprise Solution 2.2 开发帮助文档集合
首先是一个PPT文档,从宏观层面展示Enterprise Soltion的几个功能特色. Enterprise Solution解决方案安装与配置 将源代码解决方案和演示程序在电脑中进行配置,作为了解 ...