pavanarya – Pavan Kumar Aryasomayajulu

Contact Email : pavan.aryasomayajulu@gmail.com

Archive for May 2012

Getting started with MVC3 Architecture

with 3 comments

Hi today i am planning to write an article on Model-View-Controller architecture in the .net framework.
In MVC architecture we have 3 different components.

Model: A model is nothing but an object that represents some data. It can be something like a Person class that holds data like firstname,lastname,age and other information.
It can also be a dataset obtained from sql.

View: A view is nothing but an UI component that contains html content. A view takes model data and based on the data it generates the UI

Controller: A controller is a component that interacts with both view and model. A controller interacts with model and gets data from it and then it returns a view which renders the UI part.

We can understand about these components in depth going on…

Creating our first MVC project:

Note MVC3 was not provided as a template in VS2010. We are supposed to download that and install it

Here we are creating an empty mvc3 application. In the above image we can see View Engine drop down. A view engine is used to render the html content. there are multiple types of view engines like razor, webform engine, NAHTML, Spark etc.

With the empty MVC application we can see different folders. Among them Model,View and controller folders plays an important role.

Adding Our First Controller:

Now we will get a pop to enter the name(I am using FirstController) of the controller. Click on add it will add a file called FirstController.cs inside Controller folder.
FirstController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class FirstController : Controller
    {
        //
        // GET: /First/

        public ActionResult Index()
        {
            return View();
        }

    }
}

The controller we added is derived from the Controller class.

The controller is having an ActionResult called Index that retruns a view.

Adding Our First Model:
I am not doing much with the model. i am just adding a class called MyModel Class with few things like firtname,lastname.

Right click on the model folder and click on add—> Add a class file and name it as MyModel.cs
MyModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication2.Models
{
    public class MyModel
    {
        public string firstname;
        public string lastname;
    }
}

For now this is my model class.
Modifying My Controller to interact with model and then returning a view that renders html:
firstController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class FirstController : Controller
    {
        //
        // GET: /First/

        public ActionResult Index()
        {
            Models.MyModel mod = new Models.MyModel();
            mod.firstname = "pavan";
            mod.lastname = "arya";

            return View("MyFirstView",mod);
        }

    }
}

So here the index controller is interacting with the class present in the model and then it is returning the model to the view.

Creating our View:

Click on the view folder and click on add.Then enter the name and click on add.i am naming it as MyFirstView. Now we can see MyFirstView.cshtml file inside the subfolder(shared) of views folders.

MyFirstView.cshtml

@{
    Layout = null;
}
@model MvcApplication2.Models.MyModel
<!DOCTYPE html>

<html>
<head>
    <title>myFirstView</title>
</head>
<body>
    <div>
@{
    @Model.firstname;
    }
    </div>
</body>
</html>

@model MvcApplication2.Models.MyModel this specifies the model that can be used and we can use properties of that model as follows.

    <div>
@{
    @Model.firstname;
    }
    </div>

Why Do we Use MVC architecture when we have WebForms:

As of now i am not sure about the performance between MVC and web forms. But i have my own advantages that made me to use MVC

1.WebForms architecture is tightly coupled. That is we have tight coupling between aspx and aspx.cs.
2. Whenever we request for some data initially the request is routed to aspx page and from there it will interact with aspx.cs which in turn interacts with the bussinees layers and data layers. So everything is tightly coupled.

3. MVC overcomes these problems and provides loose coupling between different layers.

4 In MVC when we request for a resource it will initially hit the controller(This is taken care by routing tables in Global.ascx.cs) from there it will perform operations on the business logic and then finally it will pass return a view and can pass model object(that contains data) to the view.

5. Here we have an advantage. Suppose i am calling a controller and from there i’ll perform some business logic and based on some output from business logic i decided to render one view. let us assume i am calling a method met() in class A. This returns a boolean value and based on the boolean value i can call different views and each view renders different UI. So in this way it breaks the coupling between UI(like aspx) and code behind(like aspx.cs) files

Written by pavanarya

May 28, 2012 at 10:07 pm

Posted in MVC