What are the differences between ViewBag, ViewData and TempData?

ViewBag

  • data is stored as dynamic type
  • doesn't require explicit type conversion while reading
  • can be used to pass data only from controller to view
  • doesn't survive a redirect

ViewData

  • data is stored as Dictionary of Key Value pairs
  • data requires type conversion because the value is of type object
  • can be used to pass data only from controller to view
  • doesn't survive a redirect

TempData

  • data is stored as Dictionary of Key Value pairs
  • data requires type conversion because the value is type object
  • can be used to pass data between two controllers
  • survives a redirect

ViewBag

  • data is stored as dynamic type
  • doesn’t require explicit type conversion while reading
  • can be used to pass data only from controller to view
  • doesn’t survive a redirect

ViewData

  • data is stored as Dictionary of Key Value pairs
  • data requires type conversion because the value is of type object
  • can be used to pass data only from controller to view
  • doesn’t survive a redirect

TempData

  • data is stored as Dictionary of Key Value pairs
  • data requires type conversion because the value is type object
  • can be used to pass data between two controllers
  • survives a redirect
Default image
Sriram Mannava

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity.

Leave a Reply