Saturday, July 21, 2012

MVC ~ Transferring data from one page to another

Lot of times we would need to pass data from one page to another. The data could be a simple data or it can also be a complex data. In case of simple data, we can transfer using query string parameter and this does the job but when the size of the data is really big or if it is a complex type, then we need a different mechanism.

 

With MVC, we get the dictionary object to store the values at one end and retrieve it at the other end. These values are actually stored in session and lasts until the session terminates. Here is how we can make use of it:

 

//store the data to temp data dictionary. Data can be of any type

TempData[“myUniqueKey”] = value;

……

……

//retrieve the data from temp data dictionary.

if (TempData.ContainsKey(“myUniqueKey”))

myComplexData = TempData[“myUniqueKey”] as MyComplexData;

 

Happy Programming!!!