Friday, June 6, 2014

Difference among ViewData,TempData and View Bag in MVC

Sr.No
Temp Data
View data
View Bag
1
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
2
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
ViewData is used to pass data from controller to corresponding view.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
3
Its life is very short and lies only till the target view is fully loaded.
Its life lies only during the current request.
Its life also lies only during the current request.
4
It’s required typecasting for getting data and check for null values to avoid error.
If redirection occurs then its value becomes null.
If redirection occurs then its value becomes null.

Error.
It is used to store only one time messages like error messages, validation messages.
It’s required typecasting for getting data and check for null values to avoid error.
It doesn’t required typecasting for getting data.




No comments:

Post a Comment