What is TempData and how is it different from ViewData/ViewBag?

Junior .NET Core

Answer

ViewData and ViewBag are used to pass data from controllers to views within the same request.

  • ViewData is a Dictionary<string, object>.
  • ViewBag is a dynamic wrapper around ViewData.

TempData persists data across a single redirect and is cleared once read. It is backed by cookies or session and is ideal for messages (e.g., success notifications) that survive a redirect.

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