Communication Between Model, View, And Controller In ASP.NET

Introduction

In this article, we are going to learn how to communicate between Model, View, and Controller in ASP.NET application. We will discuss some of the communication techniques through which we can pass the data between MVC and can move between Model, View, and Controller. 

So, lets start the discussion.

What is ASP.NET?

ASP.NET was introduced by Microsoft in January 2002 with .NET Framework version 1.0. It allows programmers to build web applications, web services, and dynamic websites.

What is MVC?

MVC is Model, View, and Controller framework.

Model is the business layer of an application. It contains classes and application logic.

View is the front-end or interface through which a user interacts with our application.

Controller is the bridge between Model and View. It is used to handle requests.

Communications
Communications

Now, we are going to discuss nine different communication types that are done in Model-View-Controller.

Model to Model

We can communicate from Model to Model via parameters / composition.

Model to View

To communicate form Model to View, you have to follow the path:  Model > Controller > View

We can’t directly move from Model to View. First, the Model object is made in the Controller and then it is passed to View. We can pass the data or communicate from Model to View by these three steps:

  • Take the object in the action of a Controller.
  • Pass Model object as a parameter to View.
  • Use @model to include Model on the View page.

Model to Controller

Create an object of Model class in Controller to access the Model in Controller.

View to Model

To communicate from View to Model, you have to follow the path:  View > Controller > Model

You can’t directly move from View to Model. First, you have to submit data to the Controller and then pass it to Model. To pass the data from View to Model, you have to follow these three steps:

  • Submit HTML form to a Controller.
  • Create an object of Model in Controller.
  • Pass values to the Model object. 
View to View

We can move from one View to another View by using partial Views.

View to Controller

We can move data from View to Controller by submitting forms from View to specific Controller or by -

  • JSON
  • AJAX Calls
  • JavaScript
  • Partial Views

Controller to Model

We can move from Controller to Model just like we move from Model to Controller - by creating an object of Model in Controller.

Controller to View

We can move from Controller to View the following ways:

  • By using ViewBag
  • ViewData
  • TempData

Controller to Controller

We can move from one Controller to another by using RedirectToAction(); and then pass the name of the specific action.

Note

This article does not cover all the communication techniques through which we can pass the data or communicate between them. This article is only for beginners to understand some basic communications that are done between Model, View, and Controller.

Up Next
    Ebook Download
    View all
    Learn
    View all