Click here to Skip to main content
14,421,922 members

Data Passing Mechanism in MVC Architecture

Rate this:
4.62 (8 votes)
Please Sign up or sign in to vote.
4.62 (8 votes)
8 Oct 2014CPOL
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 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

chinmayi samal
Software Developer
India India
No Biography provided

Comments and Discussions

 
QuestionSession Data DOES expire Pin
Peter Shaw26-Oct-14 5:29
professionalPeter Shaw26-Oct-14 5:29 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun8-Oct-14 17:38
MemberHumayun Kabir Mamun8-Oct-14 17:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 8 Oct 2014

Tagged as

Stats

21.6K views
11 bookmarked