Posts Tagged ‘MVC3’

Lately I’ve been thinking about this new technology launched by Microsoft “Razor” and “MVC3 Framework” and the thought process says there are less material available on could so I thought I would post what all I know and have explored in the past 3 months. I am in the process of designing an application which is based on MVC3 with Razor as the GUI Language.

First let’s have a close look at what MVC 3 is, MVC3 is nothing but Model View Control version 3 which gives a little more flexibility over a classic MVC Application.

Model:

So what is a Model?

The Entities you define in your project that carry database objects are Models for example let’s talk about an Entity Post, a post would have Title, Body, Created Date Time and so on and following is how it should be created, there are tools available for the model to be generated automatically out of your Database and those can also be used to save time:

public class Post

{

public Post()

{

Categories = new List<Category>();

}

public virtual long Id { get; set; }

public virtual string Title { get; set; }0

public virtual string Body { get; set; }

public virtual DateTime CreationDate { get; set; }

public virtual bool IsPublic { get; set; }

public virtual IList<Core.Domain.Model.Category> Categories { get; set; }

}

Controller:

So what is a controller? Basic English says something which controls everything. Let’s take a scenario where there is a need to create / update and read posts as per our above mentioned Model so what all method would we need for such actions.

  • Create Method
  • Read Method
  • Delete Method
  • Update Method

Hence we would require a Controller page for these actions, in Visual Studio 2010 when we create a MVC3 Driven application it creates the Folder structure of our application in the following manner:

Folder Structure MVC3

Here we see a Folder for Models, Controllers, Views and Content. We have already created our Model(Entity) in the Models folder, now we will be created a Controller class. So Just go ahead and right click on Controllers and Choose Add->Controller and Visual Studio will create a class inherited from Controller Class with a default method ActionResult Method Index:

Post Controller MVC3

The Method ActionResult does nothing but encapsulates the result of an action method and is used to perform a framework level operation on behalf of the action method and returns its View, so this is what would actually  be called by the View Page.

For instance if we want to return a simple string from our Controller method to the View page, we will simply write the following and on our View Page we will call the ViewBag. Message which we will see soon.

public ActionResult Index()

{

ViewBag.Message = “Some Message”;

return View();

}

Where were we before doing this? Creating the controller class Methods. So the Controller methods for our CRUD Operation or any other generic method that we might want to call inside our application hence we will create four methods for each operation :

public ActionResult Create(MyModelName objModel)

{

//Call the Business Logic for Saving your entity

BusinessLogicClass obj = new BusinessLogicClass();

obj.Save(objModel);

return View();

}

Similarly write your Update/Delete and Read Methods.

Lets jump on to View.

View:

View is nothing but more of the UI Side of Application where we Render the output of our application, you must have heard in ASP.NET Web Application we create ASPX,ASCX,MASTER Pages. Here the Concept in Razor is a little different everything we create is going to be in .CSHTML which will help us render and show the GUI we are expecting the application to show. Following is a quick comparison:

Component Type ASP.NET Application Razor Application
User Control .ASCX registered & rendered in ASPX .CSHTML used as Partial Rendering on Razor Page
Web Page .ASPX nested in Master Page .CSHTML used as RenderBody Method on Razor Master Page
Master Page .MASTER page .CSHTML Created under Shared Folder with _Layout.cshtml or _Somename.cshtml name

Let’s check out how does a CSHTML Page looks like,while we see the Page coding you will realize how similar this is with a Classic ASP Page:

CSHTML Coding Structure

Watch the Code Closely, anything starting with @ is nothing but more like a code that you would write in C# or VB.NET, let me explain as soon as you put an @ symbol you will get Intellisense and you can choose your code.

@ViewBag.Title is a dynamice webviewpage .ViewBag property that is used to return the title from our Controller Class.

@Html.Partial is for rendering the Partial page or control as an HTML encoded string.

@RenderBody() is for rendering the portion of our content page.

Now go get started with your own MVC3 Razor Application and shoot if you have any questions. Till then Stay frosty folks J and there’s more to come in next 10 days.