Hire ASP Developer
in ASP MVC

CRUD Operation in MVC4

mvc-architecture
by | 23 Sep 2013

Review of:
Product by:
Hire ASP Developer

Reviewed by:
Rating:
5
On September 23, 2013
Last modified:October 22, 2013

Summary:

In this blog, I have explained how to CRUD (Create, Update, Delete, List) operation in MVC. Here I have used Asp.Net MVC4 Web Application using Microsoft Visual Studio 2010.

I have created User-listing Application that supports creating, editing, deleting, listing from User database.
My User table Fields is below:

Step1:-

First of select a new project–> select MVC4 web Application–>select Empty or Intranet Application.
This will create a new project. See below snapshot the folders have been added to demo application in the Solution Explorer on the right side.

Step 2:

In above snapshot, you have see different folder name like view, controller, model etc..

(1)View : In this folder you have create UI templates.

(2)Controller : Controllers respond to input from the browser, decide what to do with it, and return response to the user.

(3)Model : Model objects manipulate data ,model object retrieve and store data in database.

Step3:

First I have create folder inside views or controller –> right click to select “controller”–>controller name.

In this Application ,I have create “User” Folder inside a view folder ,I have create “CreateUserController”. You can see in below screenshot.

Step4:

After create a controller, In this snapshot you can see inside a circle , “CreateUser()” is a action method Now we are creating a view page, first of right click to “CreateUser()”Add view .You have create a view page for same name of controller methodname .like I have created CreateUser method for controller that time I have created view page same name for controller method name.

I have created a CreateUser folder inside views folder . You can see the “CreateUser.cshtml” page inside “CreateUser” folder. This page has created for insert user information.

In above snapshot ,you can see in circle how to action called in submit form. You can used form action is “Post” method.

When submit a form with user detail that time post method called. You must be add “[HttpPost]”in controller. You can see in below screenshot.

Step5:

I have used Entity data model to connection with database. Please follow below step to create a data base connection.

Right click in model folder–>add –>Add New Item–> select ADO.NET Entity Data Model follow step to wizard create database –>At last, you can create .edmx file inside model.

Note : If you do not see “Ado.Net Entity data model” please install “EF 4 CTP 5”in VS2010.

Step6: Add the DbContext Generator

Right click in model folder–>add –>Add New Item–>select ADO.NET DbContext Generator–>Follow step to wizard .

As soon as you do this, you’ll two terrifying Security Warnings – unless you click the “Do not show this message again” checkbox the first time. It will also be displayed (twice) every time you rebuild the project, so I checked the box.

Below the screenshot, I have created TestDnn.tt added in the project.

To expand model folder, you can see TestDnn.tt. You can expand “TestDnn.tt” file, see the Userdatail.cs file . To open a .cs file, see a user properties when you create a table in database.

Step7:

You have create TestDnn object in controller to use reference for userdetail object to crud operation in database.

I have used below code to insert operation using LinQ.


Step8:

same above step follow to create a operation for Edit User Detail. See a below screenshot.

Action : CreateUserCintroller

View :

Inside Createuser folder, create a view page for EditUserDetail.cshtml .you can see below snapshot to create a form inside Edit page.


Step 8: To Delete User Detail.

Below code to Delete User Record.
Controller : CreateUserController:


Step9: To List User Data. Below code to list all detail.

Controller : CreateUserController:

Create Listuser.cshtml to display all user data in view .See below snaps to bind userdata.

Leave a Reply