Written by Mark Pringle | Last Updated on Friday, November 18, 2022

MVC General Information

MVC or Model View Controller is a software architectural pattern that organizes your code into three distinct, clean, maintainable, and testable project elements (in comparison with ASP.NET Web Forms which is monolithic and does not have a separation of concerns). Simply put, the Model View Controller pattern is a way of organizing your application’s code.

MVC Design Pattern

This design pattern facilitates working on large-scale applications or websites where different project teams work together. The MVC pattern allows each team (database programmers, front-end developers, and C# or VB coders) to work on their part of the project in a neatly organized “box.”

Models

A model is a class or set of classes representing the data the application manages. Models enforce specific business rules and logic. This element of the MVC design pattern communicates with the Controller and handles the exchange of information between the database and the controller. Models do not interact directly with views.

Model Analogy

car engine

A model in the MVC pattern can be likened to a car's engine.  The engine (model) of a vehicle has specific properties.

  • Number of cylinders
  • Fuel consumption
  • Power
  • Torque
  • Compression Ratio
  • Emission Level

The engine as a model class in ASP.NET MVC would be expressed like this:

Car engine class

A model can have multiple classes that apply to an object.

Views

Views are just that: what the end-user views. They are the user interface and display the models in the structure of a table, submission form, bar chart, or other visual representation. Views contain HTML, JavaScript, and additional front-end or browser logic. Views do not interact directly with Models.

Controllers

Most of the “power” in a web application is in the Controller. Controllers retrieve data, call views, handle browser requests (get and puts), handle errors and authorization, and handle URL segments or query strings. Controllers respond to user input and actions.

Controllers send and receive data or database-specific information from Models. They also send and receive information from Views. Controllers are the gatekeepers between the Models and Views.

Finally

Simply put, the Model-View-Controller pattern helps make your web applications and web APIs testable.