In this video, learn about the model-view-controller (MVC) pattern, the responsibilities of each component in the MVC pattern, and how they work together.
- [Instructor] The model view controller pattern is a very useful design pattern that ASP.net MVC follows and actually was named for. At the time it was introduced, ASP.net consisted mostly of something called web forms applications. So the MVC moniker was tagged onto the ASP.net name to differentiate it from web forms. When I first learned MVC, I'd been doing web forms development for many years and the way that MVC instantly encouraged easy clean code felt like a breath of fresh air. I'd like to do a quick overview of the MVC pattern to make sure we're all on the same page. If you're already very familiar with this pattern, it might make sense to skip to the next chapter. For those less familiar with it, MVC is an acronym which stands for model view controller. The first and central part of the MVC pattern is the model. Models are objects that represent the data or the internal state of the application. They're also meant to contain the majority of the application's logic, though this is not always strictly the case. Views are the part of the MVC pattern that the user sees and interacts with. In a mobile or desktop application, these might be built with a presentation framework, such as WinForms or WPF. The view receives user interactions and input and passes those on to the model. The controller is like the traffic cop in an MVC application. It handles a request and routes it to the correct view and model. In an ASP.net MVC application, your controller code is usually the first code to execute for each web request. Looking at this diagram you can see how these components, the model, view, and controller, fit together. Starting at the bottom of this diagram where the user triggers a controller, you can see that the controller creates, loads, or otherwise manipulates a model object to represent the application state changes that the user requested. Then, the model class is given to the view where it's bound to the view or otherwise changes the view causing it to be updated. Finally, the view is presented to the user where they can choose to interact with the application again, restarting this cycle. Let's go through this diagram with a specific example. Imagine a user is on a view listing several products and that user wants to view the details of one of those products. That user might click a link to product name, which triggers the product controller. That controller will use the product ID from that link to load a model, perhaps a product record from the database. That model will then be bound to the product detail view, which will be sent back to the user to view and interact with again.
- Creating a new ASP.NET MVC 5 project
- Using controllers and action methods
- Creating attribute routes
- Strongly typed models
- Building HTML with HtmlHelpers
- Sharing views
- Building models with Entity Framework
- Unit testing code