Web Development (ASP.NET)

Menu

[ASP.NET MVC ] How to Pass Data From a Controller to a View

Unlike web form pages in which you can use code-behind file to access any model objects, ASP.NET MVC separates a View and a Controller. Now how can you pass data between them?

1. Passing Data From a Controller to a View

In an MVC request processing model, controllers are executed before views are rendered. It is pretty important to understand how to pass data from a controller to a view.

ViewData

ViewBag

TempData

View Model Object

Strongly-typed View Model Object

2. ViewData

The simplest way to pass data from Controllers to Views is to use the “ViewData” property.

public ViewDataDictionary ViewData { get; set; }

You can use the “ViewData” property, which is avaiable from both a Controller object and a View page. It returns a “ViewDataDictionary” object. Assign a value with a key in the controller and read the value using the key in the View page.