Search This Blog

Monday, November 7, 2016

CRUD Operations Without Writing Code In ASP.NET MVC

Introduction

This article explains how to perform CRUD database operations from .NET application without writing any code. This is possible by strong features provided by Microsoft in ASP.NET MVC. You just need to follow the steps given below.

Background

Before starting with this article, we must have knowledge of database and MVC architecture. Here we are going to use database first approach. If you don't have knowledge of database, MVC architecture and database first approach, please learn these first.

Steps

Step 1. First create a database and table tblData. In this table Id field is primary key with auto increment.

ai

Step 2. Open Visual Studio, Click on File - New Project - ASP.NET MVC4 Application. Name your project CrudOperationsWithoutCode or anything else. Click OK.

web app

Step 3. In the next window select Internet Application template and click OK,

internet app

Step 4. Right Click in your solution. Click Add - New Item. A popup window will appear.

Add - New Item

Step 5. Click ADO .NET Entity Data Model. Name it CrudEntities. Click Add, next window will appear.

ADO .NET Entity Data Model

Step 6. Select EF Designer from Database. Click Next

EF Designer from Database

Step 7.

In the next window click New Connection, a popup window will appear. Fill the required entries to connect to database. Test your connection and click OK.

New Connection

Step 8. Check Save connection settings in Web.Config as : and assign a meaningful name to it. Click Next,

config

Step 9. In the new window select Entity Framework 5.0 and click Next.

Entity Framework 5.0

Step 10. In the next window expand Tables - dbo – tblData and click Finish.

table

Step 11.

Build your solution by right clicking in solution and clicking Build. (This is necessary step because if you don't build, it will not show your model class while adding controller)

Build

Step 12. Right click in Controller folder. Click Add-Controller. New popup window will appear.

Add-Controller

Step 13. Give a meaningful name to controller. I've given CrudController Under Scaffolding option.

Template : MVC controller with read/write actions and views, using Entity Framework.
Model Class : tblData (CrudOperationsWithoutCode)
Data context class : CRUDDBEntities (CrudOperationsWithoutCode)
Views: Razor (CSHTML) and click Add

add

Step 14.

All your CRUD operation code done automatically at this stage. Now your code is ready to execute. Just Press F5 or click on execute icon as shown in screen.

execute

Output 1.

This will redirect you to Index view of HomeController. We have to hit Index view of CrudController. To do this change url at you browser as localhost:**** */Crud/Index.

index

Here you can see all records of the database are being displayed in the screen. So Read operation has been performed. Also you can see there are options like Create New , Edit, Details and Delete.

Output 2. To perform Create operation just click Create New. This will redirect you to localhost:**** */Crud/Create. You have a form to perform Create operation in database from MVC application

create

Fill the form and click Create. It will save your data to database.

Output 3. Similarly, you can perform Update and Delete operations by clicking on Edit and Deleteoptions in Index view of CrudController.

edit

delete

Points of Interest


Here you can see I've not written even a single line of code and I used powerful features of Scaffolding options provided by Microsoft and performed all create, read, update and delete operation in database from a .NET web application.

No comments:

Post a Comment