In Asp.net MVC 3, we have three objects - ViewData, VieBag and TempData to pass data from controller to view and in next request. Now question is that when to use ViewData, VieBag and TempData. All these three objects have its own importance.
In this article I am trying to expose the use of these three.
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for complex data type.
TempData
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
TempData is used to pass data from current request to subsequent request means incase of redirection.
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for complex data type and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages.
pro.shailend...
Participant
998 Points
188 Posts
When to use ViewData, ViewBag and TempData
Jul 25, 2012 05:04 AM|LINK
In Asp.net MVC 3, we have three objects - ViewData, VieBag and TempData to pass data from controller to view and in next request. Now question is that when to use ViewData, VieBag and TempData. All these three objects have its own importance. In this article I am trying to expose the use of these three.
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for complex data type and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for complex data type.
TempData
TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
TempData is used to pass data from current request to subsequent request means incase of redirection.
It’s life is very short and lies only till the target view is fully loaded.
It’s required typecasting for complex data type and check for null values to avoid error.
It is used to store only one time messages like error messages, validation messages.
For more help follow the link :
http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-objects-in-mvc3-razor.html
Blog: Dot Net Tricks Windows Apps
Please "Mark As Answer" if my suggestions helps you
anbinal
Member
4 Points
3 Posts
Re: When to use ViewData, ViewBag and TempData
Aug 29, 2012 11:15 AM|LINK
It's really nice sharing
sarvankarthi...
Member
67 Points
144 Posts
Re: When to use ViewData, ViewBag and TempData
Sep 20, 2012 09:50 PM|LINK
Greatone for freshers like me in this category (MVC)
thanks alot