MVC

Model-View-Controller

MVC is a design pattern used to decouple user-interface (view), data (model), and application logic (controller). This pattern helps to achieve separation of concerns.

Model

  • Responsible for bringing data to View.

  • Data Access Layer

  • Validation

    • Data Annotations

    • Client Validations

    • Remote Validations

View

  • Responsible for rendering UI.

  • Types

    1. WebForm View (.aspx, .ascx, .master)

    2. Razor View (.cshtml, .vbhtml)

@model - defines the type of data coming into the view

@Model - accesses the model data passed into the view

<form asp-action="Create" asp-controller="Home">

//asp-action: tells the razor what controller action we want to use
//asp-controller: is used to let our form know which controller to the find the action in
//without asp-controller, MVC will use the controller that matches the view's directory

Razor View

@{
    for (int counter = 1; counter <= 5; counter++)
    {
        <div>@counter</div>
    }
}

Controller

  • Responsible for handling every user requests.

  • Responsible for building the model and selecting a view to render.

  • responsible for sending the data to the view.

  • Methods that return IActionResult are called Action methods and provide responses usable by browsers.

Last updated