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管道进行过滤/搜索,全局错误处理,调试客 ...
随机推荐
- [BTS] WCF-OracleDB
When I insert some data to Oracle, BizTalk WCF-OracleDB throw this error. A message sent to adapter ...
- iOS僵尸对象之研究
Zombie Objects对象研究 一.Xcode 关闭ARC project -> Build settings 搜索 Automatic Reference Counting ...
- javascript设计模式与开发实践阅读笔记(7)——迭代器模式
迭代器模式:指提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示. 迭代器模式可以把迭代的过程从业务逻辑中分离出来,在使用迭代器模式之后,即使不关心对象的内部构造,也可以按顺 ...
- Git版本工具的使用
Git版本工具:Git是一个开源的分布式版本控制系统,可用于敏捷高效的处理任何或大或小的项目.详细介绍地址:https://git-scm.com/downloads.今天主要为大家分享一下怎样把本地 ...
- MySql 分页
MySql 分页 由于最近项目需要,于是就简单写了个分页查询.总体而言MySql 分页机制较为简单.数据库方面只需要使用limit即可实现分页.前后台交互就直接用session传了值. 下面就写写具体 ...
- python判断文件和目录是否存在
#Python的os.path模块提供了 isdir() 和 isfile()函数,请导入该模块,并调用函数判断指定的目录和文件是否存在. import os print os.path.isdir( ...
- [推荐] kylinPET是一款功能强大的性能测试工具
[推荐] kylinPET是一款功能强大的性能测试工具 官方网站: http://www.kylinpet.com/
- web应用程序 ---- 日志系统的设计
最近在做一个小的项目,是web的应用程序,最近也有点时间,把日志管理来简单的说说. 日志,就是需要记录一些自己感兴趣的信息,把它保存起来,具体保存在哪里?保存多长时间?这些要求都是根据不同的项目需求而 ...
- 【转】我应该直接学Swift还是Objective-C?
(本文作者Amit Bijlani,由CocoaChina翻译) 当我们发布了Swift语言学习课程之后,收到了很多邮件和私信来问自己是否还需要学习C或者Objective-C.此外,人们似乎还在迷惑 ...
- Linux安全事件应急响应排查方法总结
Linux安全事件应急响应排查方法总结 Linux是服务器操作系统中最常用的操作系统,因为其拥有高性能.高扩展性.高安全性,受到了越来越多的运维人员追捧.但是针对Linux服务器操作系统的安全事件也非 ...