How to create simple MVC 3 application for insert, delete and update functionality.

Lokesh76
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 61131 red flag
Rating: 3.75 out of 5  
 4 vote(s)

As I found many of MVC article but I don't find any article with database interaction with simple functionality. That's the reason I come out with this article.


 Download source code for How to create simple MVC 3 application for insert, delete and update functionality.

Contents

Introduction

 

        Step1:- Create MVC 3 project.

        Step2:- Create Model

        Step3:- Create Controller and Views

        Step3:- Change the Defualt controller value in Global.asax.cs

        Step 4 :- Run your application


Conclusion

 

Introduction

So In this article I have used all component of MVC (Model, View and controller). I have one database table "categories" containing ID, Name and Description columns. I have made simple mvc application to view, update and delete categories.



Step 1

In this step create a simple MVC application with empty template and razor view engine. please refer screen shot as below 
 


Step 2

After create project you find your structure as below

  

As we saw we have _ViewStart.cshtml inside Views folder so that's the startup file we have in project. beside this we have _Layout.cshtml and Error.cshtml inside Views->Shared because we use these two views as shared for all others views.

_Layout.cshtml view we used as master page for other views. So we have Views folder for containing View or html page for GUI purpose. Controller folder for all controller and Models for all Models classes.

As I'm going to create a simple application based on categories table as below
 

So start with writing a Model for this table as 
1. Create a class with name of category inside Models folder with line of codes as 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace Insert_Update_Delete_Demo.Models

{

    public class Category

    {

        public int ID { getset; }

        public String Name { getset; }

        public String Description { getset; }

    }

}


We have project name Insert_Update_Delete_Demo and create category class with getter and setter same as table schema.

2. Now we need to create a DBContext class which is going to interaction with database as 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Entity;

 

namespace Insert_Update_Delete_Demo.Models

{

    public class MesDB : DbContext

    {

        public DbSet<Category> Categories { getset; }

    }

}


In this class we have something new firstly we use Namespace System.Data.Entity because DbContext class and DbSet comes from this Namespace. This functionality part of EntityFramework code first approach. A brief about code first approach we create code first in project and its create your database schema when its run first time. This is out of context of this article.


Now we need to create a connectionString with same name as we have class name here "MesDB"

 <connectionStrings>
    <add name="MesDb" connectionString="Data Source=.\sqlexpress;Initial Catalog=MES;Integrated Security=True"
     providerName="System.Data.SqlClient" />
  </connectionStrings>


Finally we are done with Model creation for Categories table.

Step 3

Now add controller as below 

 

I have pick controller name as CategoryController, template I have pick as read/write actions and views using entity framework.

Model class I pick Category class and in Data context class pick the DbContext class. In view type i pick Razor view type. 

as soon as click Add button immediately magic begin and its create following items for me.
1. Controller with name of CategoryController 
2. Create views inside Views->Category folder for Index, Create, Edit, Details and delete functionality.

We done almost just one more thing pending in this... let have look inside Global.asax.cs file we have function for register default route as 

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }


So as function shows that default controller should be Home but in our case we have CategoryController only so our Default should be Category so change accordingly 

new { controller = "Category", action = "Index", id = UrlParameter.Optional } // Parameter defaults


We are all set now to run simple application with Insert, Update and delete functionality in Categories table.


Conclusion

This is my first article on MVC framework I want to go for long on MVC but based on feedback.... As explaining code is tough part to me so I want to enhance the same article code with other functionality of MVC.

Happy Coding....
Page copy protected against web site content infringement by Copyscape

About the Author

Lokesh76
Full Name: Lokesh Kumar
Member Level: Starter
Member Status: Member
Member Since: 3/10/2011 3:42:56 AM
Country: India
Thanks Lokesh Kumar
http://www.dotnetfunda.com/lokesh_kumar

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)