What is ViewData, ViewBag, and TempData?

Junior .NET Core

Answer

  • ViewData – A Dictionary<string, object> for passing data from controller to view in the same request.
  • ViewBag – A dynamic wrapper around ViewData for more convenient syntax.
  • TempData – Persists data across one redirect; cleared after it is read.

ViewData/ViewBag are for same-request communication; TempData is designed for redirect scenarios such as Post-Redirect-Get.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice