difference between viewdata viewbag and tempdata

What is the Difference between ViewData, ViewBag, TempData?

ViewData:
       --->It is a property of controller base class.
       --->It is of type ViewData "Dictionary".
       --->It is a Dictionary collection of string type Key and object type value.
       --->The data stored in viewdata is available only for current request ie.not accessible across requests
       syntax : 
            -->  ViewData.Add("Key",Value);
                 ViewData["Key"]=Value;

  ViewBag:
      --->It is a property of controller base class.
      --->It is of type ViewData "Dictionary".
      --->The data stored in viewdata is available only for current request 
       syntax : 
            -->  ViewBag.Name=Value;
                 @ViewBag.Name;    

  TempData:
      --->It is a property of controller base class.
      --->It is of type TempData "Dictionary".
      --->The data stored in tempdata is accessible across requests 
       syntax : 
            -->  TempData.Add("Key",Value);
                  ViewData["Key"]=Value;

Leave a comment

Your email address will not be published. Required fields are marked *