| |
|
Difference between TempData, viewData and ViewBag in MVC Question Posted on 08 May 2020 Home >> DotNet >> MVC >> Difference between TempData, viewData and ViewBag in MVC |
Difference between TempData, viewData and ViewBag in MVC
Below are the main differences between Temp data, View and View Bag
TempData
(1)class name is TempDataDictionary where TempData is derived from.
(2)We generally used TempData to pass data from current request to next request.
(3)TempData will keep the information for the time of HTTP Request. This means from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action.
(4)It genaraly requires typecasting for complex data types and checks for null values to avoid error.
(5)We use TempData to store only one time messages like the error messages and validation messages
ViewData
(1)Class name ViewDataDictionary where ViewData is derived from.
(2)ViewData is used to pass data from controller to view.
(3)It is only available for current request only.
(4)It requires typecasting for complex data types and checks for null value to avoid error.
(5)IF any redirection done then its values becomes null.
ViewBag
(1)It is a dynamic property which takes advantages of new dynamic
features in c# 4.0
(2)And ViewBag is used to pass data from the controller to respective view.
(3)It is same as ViewData means available for current request only.
(4)It will not requires typecasting for complex data type.
(5)If there some redirection occurs then its values becomes null. | |
|
|
|
|