Data Passing Mechanism in MVC Architecture






4.62/5 (8 votes)
Methodology of passing data in MVC architecture
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 ofControllerBase
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 fornull
values to avoid error. ViewData
is adictionary
object that is derived fromViewDataDictionary
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. Soviewbag
comes into the picture in those cases.Viewbag
makesviewdata
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 outmytime
property is present or not. It is the C# reflection.ViewBag
is a property ofControllerBase
class.- Its life also lies only during the current request.
- If redirection occurs, then its value becomes
null
.
public Object ViewBag { get; }
TempData
ViewData
orviewbag
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, theTempData
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 adictionary
object that is derived fromTempDataDictionary
class and stored in short lives session.TempData
is a property ofControllerBase
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 unlikeTempData
, 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 fromHttpSessionState
class.Session
is a property ofHttpContext
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.