Passing data from Controller to View in ASP.NET Core MVC

ASP.NET Core MVC is a framework that allows developers to create web applications using Model-View-Controller (MVC) architecture. One of the most important parts of the MVC pattern is the communication between the Controller and the View.

The Controller is responsible for handling user requests, processing data, and sending information to the View. The View is responsible for displaying the data that it receives from the Controller. To pass data from the Controller to the View, developers can use different approaches.

Approaches for passing data from Controller to View

ASP.NET Core MVC supports different ways to pass data from Controller to View. Each of them has a specific use case and can be used based on the scenario.

ViewData

ViewData is a dictionary object in the Controller that can store key-value pairs. It is a dynamic object that can be used to pass data to the View. The key is a string, and the value can be any object. ViewData is used to pass data to the View when the data doesn’t belong to a specific Model.


public IActionResult Index()
{
ViewData["Message"] = "Hello World!";
return View();
}

In the above code example, we have set a key-value pair in the ViewData object. We can access this data in the View using the same key.


@Html.ViewData["Message"]

ViewBag

ViewBag is similar to ViewData; it is also a dynamic object that can store key-value pairs. The only difference is that ViewBag uses a property approach instead of a dictionary. ViewBag is used to pass data to the View when the data doesn’t belong to a specific Model.


public IActionResult Index()
{
ViewBag.Message = "Hello World!";
return View();
}

In the above code example, we have set a property value in the ViewBag object. We can access this data in the View using the same property.


@ViewBag.Message

Model

Model is the most frequently used approach in ASP.NET Core MVC. It is used to pass data from Controller to View when the data belongs to a specific Model. The Model is a plain C# object that contains properties and methods. We can create a Model class and use it to pass data from Controller to View.


public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}

public IActionResult Index()
{
var student = new Student()
{
Id = 1,
Name = "John"
};

return View(student);
}

In the above code example, we have created a Model class called Student. We have initialized an instance of the Student class and set its properties. We have passed this instance of Student to the View using the return statement.


@model Student

@Model.Name

In the above code, we have declared the Model at the top of the View using the @model directive. We can access the properties of the Model using the @ sign and the property name.

FAQs

What is the difference between ViewData and ViewBag?

The only difference between ViewData and ViewBag is the approach they use to store data. ViewData is a dictionary object that stores key-value pairs, while ViewBag is a dynamic object that uses property access to store data. Both can be used to pass data from Controller to View, and both have a similar purpose.

When should I use Model instead of ViewData or ViewBag?

Model should be used when we want to pass data to the View that belongs to a specific Model. Model provides type-safe access to the data, which makes the code more readable and maintainable. ViewData and ViewBag should be used when we want to pass data to the View that does not belong to a specific Model.

What is the Model-View-Controller (MVC) pattern?

The Model-View-Controller pattern is a design pattern used to separate the concerns of an application into three components: Model, View, and Controller. The Model represents the data, the View is responsible for displaying the data, and the Controller handles user requests and updates the Model and View accordingly.

Similar Posts