Introduction
In ASP.NET MVC, there is no view state, no behind code and no server controls. As there is no view state, how will we be able to pass data in MVC architecture. There are certain mechanisms to pass data which are as follows:
ViewData
ViewBag
TempData
Session
ViewData
ViewData
is a property of ControllerBase
class.
ViewData
is used to pass data from controller to corresponding view
- Its life lies only during the current request. If redirection occurs, then its value becomes
null
. It’s required typecasting for getting data and check for null
values to avoid error.
ViewData
is a dictionary
object that is derived from ViewDataDictionary
class.
And we can use like ViewData["MyData"]
:
public ViewDataDictionary ViewData { get; set; }
ViewBag
- The syntax of
viewdata
is bit cryptic as we need to use [, " for each data. So viewbag
comes into the picture in those cases. Viewbag
makes viewdata
syntax easy.
- Basically, it is a wrapper around the
ViewData
and also used to pass data from controller to corresponding view.
ViewBag
is a dynamic property that takes advantage of the new dynamic features in C# 4.0. Dynamic property, in runtime it tries to figure out mytime
property is present or not. It is the C# reflection.
ViewBag
is a property of ControllerBase
class.
- Its life also lies only during the current request.
- If redirection occurs, then its value becomes
null
.
public Object ViewBag { get; }
TempData
ViewData
or viewbag
does not maintain data from action to action or controller to action. These maintain data from controller to view only. So in order to persist data for the whole request, the TempData
can be used.
TempData
is used to pass data from current request to subsequent request meaning redirecting from one page to another.
- Its life is very short and lies only till the target view is fully loaded.
- Its required typecasting for getting data and check for
null
values to avoid error.
TempData
is a dictionary
object that is derived from TempDataDictionary
class and stored in short lives session. TempData
is a property of ControllerBase
class.
public TempDataDictionary TempData { get; set; }
Session
- For a complete request that is from controller to controller or action to action and also for the subsequent requests, the data can be persisted with the
session
variable.
Session
is also used to pass data within the ASP.NET MVC application and unlike TempData
, it never expires.
Session
is valid for all requests, not for a single redirect.
- It’s also required typecasting for getting data and check for null values to avoid error.
Session
is an object that is derived from HttpSessionState
class. Session
is a property of HttpContext
class.
public HttpSessionState Session { get; }
Here, we can see there are many options to pass data in MVC application. In order to make the full utilization of memory and garbage collector, we need to choose our data passing variables as per our needs.
Hope the above documentation for the data passing variables in MVC will be helpful.