Blogs

Blogs

What is MVC?

22nd April 2015

What is MVC?

MVC stands for Model View Controller.

So, what purpose do each of these have you ask!

  • The Model this contains all of your classes. Each of these classes should be used to hold the data / business object for your program.
  • The View this is where you enter your code for what you want to display to the end user such as your HTML.
  • The Controller this is where the majority of your logic will reside, such as getting data from the model and passing it to the view.

By default MVC 3 and newer uses Razor as the view engine. Razor is based on C# however you can use VB with it if you like

The Razor view engine allows you to quickly and easily start rendering content out to your end user. In Razor you can enter your C# straight into your view, here is an excerpt from my Index page:).

@{
    foreach (var contentItem in Model.PageContents)
    {                
        <div class="col-md-12">
            <h2>
                @contentItem.Title
            </h2>
            <div class="col-md-12">
                @contentItem.Content
            </div>
        </div>
    }
}
As you can see I am using a foreach to go through all of the items in Model.PageContent, and then I render out the title and content of each item.

How did the view get this data though?

For that we will take a look at my HomeController.
/// <summary>
/// Home Controller
/// </summary>
public class HomeController : Controller
{
    /// <summary>
    /// Index view
    /// </summary>
    /// <returns>action result</returns>
    public ActionResult Index()
    {
        return this.View(this.GetWelcomeContent());
    }

    /// <summary>
    /// Gets the welcome content.
    /// </summary>
    /// <returns>welcome content</returns>
    private WelcomeContent GetWelcomeContent()
    {
        var welcomeContent = new WelcomeContent();
        welcomeContent.RssItems = RssItem.GetRssFeedContent();
        welcomeContent.PageContents = PageContent.GetPageContents();

        return welcomeContent;
    }
}

In my Index method I call another method which creates a new variable and then loads all of the data necessary by calling other methods. Then once I have all of the data I return it, which then passes it to my view for the data to be rendered out.  

In my GetWelcomeContent I create a new instance of my WelcomeContent class which is a Model, let’s go take a look at it.

/// <summary>
/// Welcome Content
/// </summary>
public class WelcomeContent
{
    /// <summary>
    /// Gets or sets the RSS items.
    /// </summary>
    /// <value>
    /// The RSS items.
    /// </value>
    public List<RssItem> RssItems { get; set; }
        
    /// <summary>
    /// Gets or sets the page contents.
    /// </summary>
    /// <value>
    /// The page contents.
    /// </value>
    public List<PageContent> PageContents { get; set; }
}

As you can see it is a very simple class it only has two properties, which are both lists. Below is the code for my PageContent class (Model)

/// <summary>
/// Page Content
/// </summary>
public class PageContent
{
    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>
    /// The title.
    /// </value>
    public string Title { get; set; }

    /// <summary>
    /// Gets or sets the content.
    /// </summary>
    /// <value>
    /// The content.
    /// </value>
    public string Content { get; set; }

    /// <summary>
    /// Gets the page contents.
    /// </summary>
    /// <returns>page contents</returns>
    public static List<PageContent> GetPageContents()
    {
        var pageContents = new List<PageContent>();

        //// Load data from db, removed the code so that this more succinct

        return pageContents;
    }
}

I have removed the code that loads the data from SQL to keep the code as short as possible for this post.

But how did it initially know to call my Index method in my HomeController I hear you cry! It knew which method to call because of the way that the RouteConfig was setup, please see below.

/// <summary>
/// Registers the routes.
/// </summary>
/// <param name="routes">The routes.</param>
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new 
            { 
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional 
    });
}

Above you can see that it will always look for a controller and then the action with an optional id.

An example of the url would be something along the lines of: http://websiteurl/Home/Index

That is a very simple example of what MVC is and how it all links together, I hope that you found it useful. If you would like some more in-depth posts about MVC or if you have any specific questions then please feel free to leave a comment.

Useful links:

http://www.asp.net/mvc/overview

http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.