ViewBag, ViewData and TempData Simplified

Here are some differences between ViewBag, ViewData and TempData when used in an ASP.NET Core MVC application.

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

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

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