Friday 12 October 2012

MVC Programming Model

MVC is one of three ASP.NET programming models.
MVC is a framework for building web applications using a MVC (Model View Controller) design:
     •    The Model represents the application core (for instance a list of database records).
     •    The View displays the data (the database records).
     •    The Controller handles the input (to the database records).
The MVC model also provides full control over HTML, CSS, and JavaScript.

The MVC model defines web applications with 3 logic layers

 

  1.        The business layer (Model logic)
  2.        The display layer (View logic)
  3.         The input control (Controller logic)

 Step1:- Create project

 

Once you click ok, you have a ready made structure with appropriate folders where you can add controllers, models and views.

Step 2:- Add controller

Once you add the new controller you should see some kind of code snippet as shown in the below snippet.

public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}

Step 3:- Add View 

Now that we have the controller we need to go and add the view. So click on the Index function which is present in the control and click on add view menu as shown in the below figure.

The add view pops up a modal box to enter view name which will be invoked when this controller is called as shown in the figure below. For now keep the view name same as the controller name and also uncheck the master page check box.

Once you click on the ok button of the view, you should see a simple ASPX page with the below HTML code snippet. In the below HTML code snippet I have added “This is my first MVC application”.

Step 4:- Run the application


 If you do a CNTRL + F5 you should see an error as shown in the below figure. This error is obvious because we have not invoked the appropriate controller / action.
If you append the proper controller on the URL you should be able to see the proper view.


 So have a toast of beer for your first ASP.NET MVC application.