Introduction to MVC
Last updated
Last updated
The Model-View-Controller (MVC) architectural pattern separates an application into three main groups of components: Models, Views, and Controllers.
This pattern helps to achieve separation of concerns.
Using this pattern, user requests are routed to a Controller
Controller is responsible for working with the Model to perform user actions and/or retrieve results of queries.
The Controller chooses the View to display to the user and provides it with any Model data it requires.
Model-View-Controller (MVC) architectural pattern to achieve separation of concerns. This helps you scale the application in terms of complexity because it's easier to code, debug, and test something has a single job!
The following diagram shows the three main components and which ones reference the others:
Note that both the view and the controller depend on the model. However, the model depends on neither the view nor the controller.
The Model in an MVC application represents the state of the application and any business logic or operations that should be performed by it. Business logic should be encapsulated in the model, along with any implementation logic for persisting the state of the application.
Strongly-typed views typically use ViewModel types designed to contain the data to display on that view. The controller creates and populates these ViewModel instances from the model.
Model and ViewModel are different and both server different purposes!
Views are responsible for presenting content through the user interface. They use the Razor view engine to embed .NET code in HTML markup. There should be minimal logic within views, and any logic in them should relate to presenting content.
There should be minimal logic within views, only pertaining to display operations.
Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render.
In an MVC application, the view only displays information; the controller handles and responds to user input and interaction.
In the MVC pattern, the controller is the initial entry point, and is responsible for selecting which model types to work with and which view to render
Hence controller gets its name - it controls how the app responds to a given request.
Controllers shouldn't be overly complicated by too many responsibilities. To keep controller logic from becoming overly complex, push business logic out of the controller and into the domain model. If you find that your controller actions frequently perform the same kinds of actions, move these common actions into filters.
The ASP.NET Core MVC framework is an open-source framework by Microsoft
It is optimized for use with ASP.NET Core
ASP.NET Core MVC provides a patterns-based way to build dynamic websites that enables a clean separation of concerns
It gives you full control over markup
It supports TDD-friendly development and uses the latest web standards.
ASP.NET Core MVC framework offers several useful features out of the box, like Model binding, Validation, Dependency Injection, Routing, Filters and Identity Framework.
We will learn more about them as we progress through the course. For now, you can look into the MS documentation for further info: