In ASP.NET MVC 3 and MVC4, we have the objects ViewData and TempData to pass data from a controller to a view. I’ll use a sample application to show how these properties works.

TempData


First I’ll explain TempData that is driven from a TempDataDictionary. I’ll show you the functionality with an example.

The end result is that any data stored in TempData will be around for the life of the current request and the next request only, or until the item is removed.

To show the practical usage of View Data, I provide the following sample application:

Step 1:

View Data

If you notice from the image above, it has the keys count =1 and has the value EmployeeRegistration.

Step 2:

After redirecting to the View from the controller, we can use the TempData value at the View page like this.

Note: Kindly check that the value is always null and make it a best practice to use that in code to reduce the occurrences of errors.

TempData

Step 3:

Now we’ll determine whether the tempData value persists at the next level.

I’ll hit the other action method to verify this. In our case I call the details action method:

TempData value

See the result is as depicted in the image below:
TempData using Breakpoint

It’s magic. But this magic happens because of the statement in code TempData.Keep("EmployeeRegistration");

TempData.Keep () has two overloaded method and I’ve used one of them.

Note: We can understand TempData as the Session we used in ASP.NET.

Usage of TempData

Usage 1: If you want to keep TempData values even after reading them then call Keep().Then those values will be kept for next request also.

TempData.Keep("EmployeeRegistration");

Usage 2: If you want to remove TempData values then call Remove (). Then those values will be removed from the current and next request.

TempData.Remove("EmployeeRegistration");

Conclusion

We should use TempData for a subsequent request only to pass Controller data to a View and later also.

ViewData

ViewData that is driven from a ViewDataDictionary. I’ll show you the functionality with an example.There is a little snippet about ViewData as shown below:

To show the practical usage of View Data, I provide the following sample application:

Step 1:

ViewData using Breakpoint

If you notice from the image above, it has the keys count =1 and has the value EmployeeRegistration.

Step 2:

After redirecting to the View from the controller, we can use a ViewData value at the View page like this.

Note: Kindly check that the value is always null and make it a best practice to use that in code to reduce the occurrences of errors.

Check ViewData null value

Step 3:

Now we’ll determine whether or not the ViewData value is persisted at the next level.

I’ll hit the other action method to verify this. In our case I call the details action method:

ViewData value

See the result is as depicted in the image below:

View Data with breakpoint

It doesn’t retain its ViewData value and has count=0.

Conclusion

We should use ViewData for a current request only to pass Controller data to the View.