Type Conversion code is required while enumerating Its value becomes null if redirection has occurred. Same as ViewData TempData is used to pass data between two consecutive requests. It lies only during the current request. It is Key-Value Dictionary collection. ViewData is a dictionary object and it is property of ControllerBase class. ViewBag is Dynamic property of ControllerBase class.
ViewData is Faster than ViewBag. ViewBag is slower than ViewData. ViewData is introduced in MVC 1. ViewBag is introduced in MVC 3. TempData is also introduced in MVC1.
ViewBag only works with. TempData also works with. Type Conversion code is required while enumerating. Although outdated, this article has good description of the TempData lifecycle.
As Ben Scheirman said here :. TempData is a session-backed temporary storage dictionary that is available for one single request.
Data can be preserved in the TempData property dictionary of the controller for the duration of a single HTTP redirect request. This behavior is new from ASP. In earlier versions of ASP.
One gotcha I came across is that TempData values are cleared after they are read by default. There are options, see methods 'Peek' and 'Keep' on Msdn for more info. Example Controller :. TempData transfers the data between controllers or between actions. It is used to store one time messages and its life is very short. Keep to make it available through all actions or to make it persistent. Just a side note to TempData. Data in it is stored not stored until the next request , but until the next read operation is called!
See: TempData won't destroy after second request. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Ask Question. Asked 13 years ago. ViewData is introduced in MVC 1. ViewBag is introduced in MVC 3. TempData is also introduced in MVC1. ViewBag is a dynamic property , so there is no need for typecasting. Its value also becomes null if a redirection has occurred. TempData is used to pass data between two consecutive requests. TempData only works during the current and subsequent request.
Share your valuable feedback, please post your comment at the bottom of this article.
0コメント