Showing posts with label TempData. Show all posts
Showing posts with label TempData. Show all posts

Tuesday, March 5, 2019

Passing data from Controller to View Examples

In the previous post we saw different ways in which we can pass data / objects from the controller to the View. In this post we will see a simple example on how to assign values in the controller and use them in the View using the different methods ViewData, ViewBag and TempData.

In the controller, we assign string and objects to the ViewData, ViewBag and TempData as follows.

Passing data from Controller to View

There are different ways to pass values / objects from a controller to a view in Asp.Net Core MVC. Following are some of the commonly used ways.

ViewData – Returns a ViewDataDictionary, allows us to store key-value pairs in the controller and access them in the view. The key is a case sensitive string and the value can be any type int/string/object etc. When we use ViewData to pass custom objects we need to cast them in the View before using the properties.