Loading


What is MVC?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.

MVC design pattern was introduced in the 1970s that basically divides an application into 3 major components as Model, View, and Controller. The main objective of the MVC design pattern is the separation of concerns (codes), which means the domain model and business logic are separated from the user interface (i.e. view). As a result, maintenance and testing of the application become simpler and easier.

How does the MVC Design Pattern work in ASP.NET MVC Application?

Let us understand how does the MVC Design Pattern work in the ASP.NET MVC application with an example. Let say we want to display the student details on a web page as shown in the below image.

How does the MVC Design Pattern work in ASP.NET MVC Application

So, when the client (user) issues a request something like “http://dotnettutorials.net/student/details/2” from a web browser then the request is handled by the MVC framework as shown below.

ASP.NET MVC Framework Model View Controller Diagram

The controller is the component in the ASP.NET MVC application that actually receives the incoming HTTP request and then handles that request. In order to handle the incoming HTTP request, the controller does several things are as follows.

  1. The controller creates the model object that is required by a view. The model is the component in the MVC design pattern that contains a set of classes to represent the domain data or business data as well as logic to manage the data.
  2. The controller then selects a view to render the domain data or business data. The point that you need to remember is, while selecting a view, it is the responsibility of the controller to pass the model data.
  3. In the MVC Design Pattern, the one and only responsibility of a view is to display the model data. So, the responsibility of a view is to generate the necessary HTML which will render the model data or business data. Once the HTML is generated by the view, then that HTML is then sent to the client via the controller who initially made the request.

Now, I hope you understand the basic idea of the MVC Design Pattern. Let us discuss each of the components of the MVC design pattern in detail by comparing it with our example.

Model:

The Model is the component in the MVC design pattern that manages that business data or domain data i.e. state of the application in memory. The Model contains a set of classes that represent the data as well as logic to manage the data. So, in our example, the model is consists of Student class to represent the student data as well as StudentBusinessLayer class to retrieve the student data from any persistent medium like a database. 

Model in ASP.NET MVC Application

So in short, a Model:

  1. In ASP.NET MVC is basically a C# or VB.net class to represent the data as well as to manage the data.
  2. It is accessible by both controller and view.
  3. It can be used to pass data from controller action methods to a view.
  4. It can also be used by a view to display data on a page (HTML output).
View:

The view is the component in MVC Design Pattern which renders the model data as the user interface with which the end-user can interact. So, the View creates the user interface with data from the model. In our example, we want to display the Student information on a web page. So here the student model carried the student data to the view. This is the student model which should be supplied by the controller to the view. The following code does the same thing.

@model FirstMVCApplication.Models.Student
<html>
<head>
<title>Student Details</title>
</head>
<body>
<br />
<br />
<table>
<tr>
<td>Student ID: </td>
<td>@Model.StudentID</td>
</tr>
<tr>
<td>Name: </td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Gender: </td>
<td>@Model.Gender </td>
</tr>
<tr>
<td>Branch: </td>
<td>@Model.Branch</td>
</tr>
<tr>
<td>Section: </td>
<td>@Model.Section </td>
</tr>
</table>
</body>
</html>

So in short, a View

  1. In ASP.NET MVC is a cshtml page.
  2. It contains all page-specific HTML generation and formatting code.
  3. A request to a view can only be made from a controllers action method.
  4. The one and only responsibility of a view is to render the domain data or business data.
Controller:

The Controller is the component that contains the control flow logic. It is the one that will interact with both models and views to control the flow of application execution. The controller is the component in MVC Design Pattern that will handle the incoming HTTP Request. Based on the user actions, the respective controller will work with the model and view and then sends the response back to the user who initially made the request. In our example, when the client issued a request to the following URL

http://dotnettutorials.net/student/details/2

Then that request is going to be mapped to the Details action method of the Student Controller. Following is the code of our Controller class with the Details action method.

Controllers in ASP.NET MVC

So, in short, a Controller:

  1. Is basically a C# or VB.NET class that is inherited from the System.Web.Mvc.Controller.
  2. Is the component which will interact with both Models and views.
  3. Contains action methods that are responsible for handling the incoming HTTP Request.
  4. Can access and use the model class to pass the data to the views.
Advantages of using ASP.NET MVC to develop Web Application
  1. It is lightweight because it does not use view state or server-based forms or server controls.
  2. Each developer based on his expertise or experience can work on different parts of the application. For example, one developer may work on the view while the second developer can work on the controller logic and the third developer may work on the business logic.
  3. Clean HTML and easy integration with javascript and jQuery.
  4. It provides better support for test-driven development (TDD). This is because we can focus on one aspect at a time i.e. we can focus on the view without worrying about business logic.
  5. ASP.NET MVC Framework divides the application into three main aspects such as Model, View, and Controller which make it easier to manage the application complexity.
  6. Another important advantage of the ASP.NET MVC framework is its components are designed to be extensible and pluggable and therefore they are easily replaced or customized.
  7. The MVC framework is built on top of the ASP.NET Framework and hence we can use most of the ASP.NET features such as authentication and authorization scenarios, membership and roles, caching, session, and many more.
  8. ASP.NET MVC framework supports a powerful URL routing mechanism (i.e. attribute routing) which helps to build more user-friendly and SEO-friendly URLs for our application.

See All

Comments (261 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /261

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /261

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /261