Manage Session In MVC

Getting Started

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage sessions, apart from that we can use session variable, hidden fields and HTML controls for doing it. But like session variable these elements could not preserve values for all request, value persistence varies depending the flow of request. For example ViewData maintains data when you move from controller to view only

TempData

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary. 

TempData internally uses session variable and stays for a subsequent HTTP Request. This means it maintains data when you move one controller to another controller or one action to another action. As this is a dictionary object null checking and typecasting is required while using it.

Below example shows how to retrieve and put data from or into tempdata both in controller and view .
  1. //storing data into tempdata  
  2. TempData["Name"] = "Kailash";  
  3. TempData["height"] = 8.5;  
  4. //retrieving data from tempdata in controller  
  5. string name = TempData["Name"].ToString();  
  6. double height = Convert.ToDouble(TempData["height"]);  
  7. //retrieving data from tempdata in view using asp.net enging  
  8. < label > <%TempData["Name"].ToString();%> < /label> < label > <%Convert.ToDouble(TempData["height"]); %> < /label>  
  9. @if(!TempData["Name "] = null)  
  10. {  
  11.     @TempData["Name "];  
  12. }  
  13. @if(!TempData["height "] = null) {  
  14.     Convert.ToDouble(@TempData["height"]);  
  15. }  
TempData gets destroyed immediately after it’s used (once value is read from tempdata) in subsequent HTTP request, so no explicit action required, if you want preserve value in the subsequent request after using need to call keep method or peak method. See the below example.

ViewData

ViewData maintains data when you move from controller to view. It is also a dictionary object and derived from ViewDataDictionary. As Data is stored as Object in ViewData, while retrieving, the data it needs to be Type Casted to its original type as the data is stored as objects and it also requires NULL checks while retrieving.

Below example shows how to retrieve and put data from or into ViewData both in controller and view .
  1. //storing data into viewdata  
  2. ViewData["ViewDataName"] = "abc";  
  3. ViewData["ViewDataHeight"] = 2.56;  
  4. //retrieving data from viewdata in controller  
  5. string abc = ViewData["ViewDataName"].ToString();  
  6. double ViewDataHeight = Convert.ToDouble(TempData["ViewDataHeight"]);  
  7. //retrieving data from viewdata in view using asp.net enging   
  8. < label > <%ViewData["ViewDataName"].ToString();%> < /label> < label > <%Convert.ToDouble(ViewData["ViewDataHeight"]); %> < /label>  
  9. @if(!ViewData["ViewDataName "] = null)   
  10. {  
  11.     @ViewData["ViewDataName "];  
  12. }  
  13. @if(!ViewData["ViewDataHeight "] = null) {  
  14.     Convert.ToDouble(ViewData["ViewDataHeight"]);  
  15. }  

ViewBag

The ViewBag is a dynamic type property of ControllerBase class which is the base class of all the controllers. It’s a dynamic wrapper around ViewData casting is not required when you use ViewBag. ViewBag only transfers data from controller to view, not visa-versa. ViewBag values will be null if redirection occurs.

ViewBag support any number of properties or values. if same value found then it will only consider last value assigned to the property.

Below are various examples of ViewBag.
  1. //storing data into viewdata  
  2. ViewBag.Name = "Kailash";  
  3. ViewBag.Height = 2.24;  
  4. //retrieving data from viewdata in controller  
  5. string ViewBagname = ViewBag.Name;  
  6. double ViewDataHeight = ViewBag.Height;  
  7. //retrieving data from viewdata in view using asp.net enging  
  8. @ViewBag.Name;  
  9. @ViewBag.Height;  
  10. @Html.DropDownList("accountid"new SelectList(ViewBag.Accounts, "AccountID""AccountName"))  
  11. @Html.DropDownListFor(x => x.accountId, new SelectList(ViewBag.Accounts, "AccountID""AccountName"))  
As like ASP.NET session and Hidden fields can be used. session variables we can maintain data from any entity to any entity and hidden fields help to maintain data from UI to controller only. So you can send data from HTML controls or hidden fields to the controller using POST or GET HTTP methods.

Summary

Hope this article will help you to get an idea about session management in MVC. Enjoy and happy coding.