ASP Module 2
Getting Started with MVC
Getting Started with MVC
  • Overview
    • Introduction to MVC
  • Understanding MVC
    • Creating an MVC app
    • Add a controller
      • Understand HTTP endpoint
      • Understand basic routing
    • Add a view
      • Understanding shared layouts
      • Setting view title
      • Passing data to view
  • Exercise
    • Add two numbers
      • Using Model
  • Display Movies
    • Add a model
      • Scaffold the index view
      • Scaffold the movies controller
      • Add temp movie data
      • Pass data to index view
    • Implement actions
      • The first create action
      • The second create action
      • The details action method
      • Edit & Delete actions
      • Asynchronous actions
    • Repo link
Powered by GitBook
On this page
  • What is MVC?
  • Model Responsibilities
  • View Responsibilities
  • Controller Responsibilities
  • What is ASP.NET Core MVC?
  1. Overview

Introduction to MVC

NextCreating an MVC app

Last updated 2 years ago

What is MVC?

  • 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:

MVC Pattern

Note that both the view and the controller depend on the model. However, the model depends on neither the view nor the controller.

Model Responsibilities

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.

Then what is ViewModel?

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!

View Responsibilities

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.

Controller Responsibilities

  • 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.

Do not overload the controllers!

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.

What is ASP.NET Core MVC?

  • 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:

Overview of ASP.NET Core MVCdocsmsft
Logo