MVC Pattern

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:06:00

What is an MVC web application?

An MVC web application is built using a model-view-controller software design architecture, that helps in the development of user interfaces, data, and business logic.

What is MVC?

The Model-View-Controller (MVC) software design pattern was created in the 1970s. It promotes the separation of concerns by isolating the domain model and controller code from the user interface (view), which simplifies maintenance and testing.

Describe the MVC Design Pattern

The MVC design pattern divides an application into three major components: Model, View, and Controller

Model

The Model is a collection of classes that describe both the business logic (business model) and the data access activities (data model). It also sets business rules for data, specifying how it can be altered and manipulated.


Key Concepts of Model

  • Model Classes: These classes reflect the data structure. They are normally created in the ASP.NET MVC project's Models folder.
  • Entity Framework(EF): A common ORM (Object-Relational Mapping) that is used with ASP.NET MVC for database operations.

Creating a Model

  • Define the Model Class: Create a class under the Models folder.
  • Add Data Annotations: Data annotations can help to enforce validation requirements.

Model Binding

Model Binding automatically converts form inputs into model properties. In controllers, model binding is used to connect form data to model parameters in action methods.

ViewModel

The ViewModel class is designed exclusively for views. It can mix properties from many models or add new characteristics required for the view.

View

The View represents UI components such as CSS, jQuery, and HTML. It is solely responsible for showing the data received from the controller as the result. This also converts the model(s) to UI.


Why is View required in ASP.NET MVC?

The view is necessary to display the user interface on the browser. It's necessary to make your application engaging and user-friendly. To use a client-side framework such as AngularJS, ReactJS, etc.

Type of View in ASP.NET MVC

Normal View

A Normal View in ASP.NET MVC renders HTML content for the browser, much like a webpage in ASP.NET WebForms. It is used to provide data to the user and usually comprises HTML, Razor syntax, and server-side code.

Partial View

A Partial View is similar to user control in ASP.NET Web Forms. It is a reusable view component that can be embedded into other views and is useful for rendering web page parts such as headers, footers, and reusable widgets.

Layout View

A Layout View works similarly to a master page in ASP.NET Web Forms. It creates a uniform structure for many views by defining common HTML elements (such as headers, footers, and navigation menus) that are shared across the application's pages.

Common View File Extensions in ASP.NET MVC

The view includes the following language-dependent extension:

  • .aspx
  • .asp
  • .html
  • .cshtml
  • .vbhtml

Steps to Create an ASP.NET MVC Application Using Visual Studio 2017

  • First, open Visual Studio 2017 and create an ASP.NET MVC application called "MVC5ViewsDemo".
  • Go to Solution Explorer and right-click on the "Controller" folder >> Click on [Add] >> "Controller"Go to Solution Explorer => Views Folder. Right-click the "Home" folder >> select "Add" >> Click [New Item].
  • MVC 5 View Page (Razor) template from the "Add New Item" window and provide the needed name, such as "Index.cshtml", then click the "Add" button.

Create a View in ASP.NET MVC 5

  • Go to Solution Explorer => Views Folder. Right-click the "Home" folder >> select "Add" >> Click [New Item].
  • MVC 5 View Page (Razor) template from the "Add New Item" window and provide the needed name, such as "Index.cshtml", then click the "Add" button.

Controller

The Controller is responsible for processing incoming requests. It gets input from users via the View, processes the data using the Model, and returns the results to the View. Typically, it serves as a liaison between the View and Model.


Key Concepts of Controller Class

Controller class

Controllers can be defined by generating classes that derive from the System.Web.Mvc.Controller is a base class. Each controller class name normally ends in "Controller" (for example, HomeController).

Action Methods

Action Methods refer to methods found within a controller class. Action methods process HTTP requests and return an ActionResult. Common ActionResult types include ViewResult, JsonResult, RedirectToRouteResult, and so on.

Routing

ASP.NET MVC uses a routing process to connect URLs to controller operations. Routes are defined in the RouteConfig class, which is normally located in the App_Start folder.

ActionResult

An action method produces a result. It can be of several types:

  • ViewResult: Displays a view.
  • JsonResult: Returns JSON-formatted data.
  • RedirectToRouteResult: Navigate to another action.
  • ContentResult: Provides a plain text result.
  • FileResult: Returns the file to be downloaded.

Model Binding

ASP.NET MVC automatically converts HTTP request data into action method parameters. It supports complex type parameters, simple types, and collections.

Filters

Filters are used to run code before or after action methods. Filters are classified as Authorization, Action, Result, or Exception. Filters are applied using attributes such as [Authorise], [HandleError], and [OutputCache].

Dependency Injection

ASP.NET MVC provides dependency injection, which improves the separation of concerns and testability. It can be configured in Startup.cs or using IoC containers such as Autofac, Ninject, and so on.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this