Skip to content
Nubgrammer
Menu
  • Home
  • Contact Me
Menu

What is MVC?

Posted on October 31, 2017November 10, 2017 by Tyler Sells

TL;DR:

MVC = Model View Controller.  Data from a database is represented as a model class.  The view takes that data represented by the model and inserts it into an HTML template that is written into the view.  The controller is responsible for creating the model from data retrieved from a database and passing that model into the view.

It’s an Architectural Pattern:

MVC stands for Model View Controller.  It is an architectural pattern that many other frameworks use, not just ASP.NET.  Spring MVC, Node.js MVC frameworks, and Ruby On Rails MVC frameworks are just a few of the other languages that support the MVC pattern.  However, my experience is primarily with ASP.NET so this post will be based on ASP.NET MVC but it should apply to most other MVC frameworks.

The primary goal of MVC is something called Separation of Concerns.  I’ll cover that in more detail later on, but in a nutshell, SOC is the idea that a unit should do one thing, only one thing, and do it as efficiently as possible.  MVC accomplishes this by delegating important functions of web applications into three separate concerns.

The Model:

The Model is just a class (at least in ASP.NET) that typically represents an item in a database table.  It contains variables that directly correspond to the schema of that table.

This is what the Products table in a project I’m working on looks like:

Products Table in DB

This is what the C# model that represents that table looks like:

public class Product
{
     public int ID { get; set; }
     public int BrandID { get; set; }
     public int ModelID { get; set; }
     public string InfoPage1 { get; set; }
     public string Notes { get; set; }
}

 

The View:

The View is a file that contains a display layout.  They are designed to stand alone as a full page, or they can act as partial pages.  Think of them as a template for displaying information to the user.  In the case of a partial view, you can have as many partial views as you need in a single parent view (Yes, they are nest-able).  When a view is created, if it requires table data (hint: they don’t always have to have data!), the data is passed into it and can be used to modify the view to represent that particular table row (aka Model).

My view for my product model looks like this:

@model IEnumerable<InventoryManagementSystem.Models.Product>
<table class="table">
     @foreach (var item in Model) {
          <tr>
               <td>
                    @Html.DisplayFor(modelItem => item.BrandID)
              </td>
              <td>
                    @Html.DisplayFor(modelItem => item.ModelID)
              </td>
              <td>
                    @Html.DisplayFor(modelItem => item.InfoPage1)
              </td>
              <td>
                    @Html.DisplayFor(modelItem => item.Notes)
              </td>
          </tr>
     }
</table>

This view incorporates something called Razor syntax to hold the model and pull out the necessary information.  It uses standard HTML to create the rest of the template.  This view simply looks through a list of models and prints them all out into an HTML table.

The Controller:

The Controller is the magic that puts it all together.  You might even say it *ahem* controls the application.  What I mean by that is when a user initiates some kind of event on your web app such as clicking a navigation link or submitting a form, the controller is responsible for actually handling that event.

When clicking a link, we expect to go to a different page.  A method in the controller is called that returns the view designated for that page and sends it back to the user.  These methods are referred to as actions.  In the case of a form submit, let’s assume we’re changing the name of someone in a database.  When the user presses submit, an action is called in the controller that takes that form data in the form of a model and makes the necessary changes to the database table based on that model.  It can either return a new view at this point or just return some kind of status message back to the user.

This is what one of my controller actions looks like for the Product model and view:

public ActionResult ListProducts()
{
     var products = db.Products.ToList();
     return PartialView(products);
}

db.Products.ToList() goes to the database, finds all of the items in the Products table and returns them as a list of product models.  Next, it returns a partial view that accepts that list as a parameter.  This is where the view gets it’s data to plug into the template.

Let’s look at a chart:

Hopefully you now have a better understanding of MVC.  Lastly, I want to add this a flow chart that shows how all of this looks in practice:

MVC Flowchart

 

User Action:  Let’s say the user clicks on a link to take him to the page that lists all of the products in our database.

Step 1:  The page tells the controller to perform the action that returns the Product Detail View.

Step 2:  The controller might do some logic first but ultimately, it goes to the database, retrieves the appropriate data.

Step 3:  The controller creates an instance of the Product model class, and since we are wanting ALL of the products in the database, it will create an instance for every product that it found in the database.

Step 4:  The controller passes all of the model instances into the view which incorporates the model data into the HTML that is returned to the controller.

Step 5:  The controller sends that view back to the user to display the new page.

That’s MVC 

Hope you guys enjoyed reading this.  If you’ve got anything to add or need clarification, or just want to tell me I screwed something up, let me know in the comments or over at the forums!

Author: Tyler Sells

Github

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • More
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Reddit (Opens in new window) Reddit

Like this:

Like Loading...

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow me on Twitter

My Tweets

Github Repos

vtsells (Tyler Sells)

Tyler Sells

vtsells
http://www.nubgrammer.com
Joined on Jun 21, 2017
9 Public Repositories
100DaysOfCode
embers
MultiSelect
MVC-Project-Start
nubgrammer.com
PermIT
Spray
vtsells.github.io
Wizard
0 Public Gists

Categories

  • #100DaysOfCode (4)
  • ASP.NET (7)
  • ASP.NET Core (1)
  • ASP.NET MVC (3)
  • CSS (4)
  • General (13)
  • JS (3)
  • LESS (2)
  • Snippets (4)
  • Tools (4)
  • Tutorials (9)

Recent Posts

  • Creating a Knockout.js project on Codepen
  • 100DaysOfCode Day 3 – A State of Mind
  • 100DaysOfCode Day 2 – The Building Blocks
  • 100DaysOfCode Day 1 (Sort of cheated already)
  • Committing to #100DaysOfCode
© 2025 Nubgrammer | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
%d

    Privacy Policy