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
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 belowStep 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 { get; set; }
public String Name { get; set; }
public String Description { get; set; }
}
}
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 { get; set; }
}
}
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
No comments:
Post a Comment