Skip to main content

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

Junior .NET Core
Quick Answer TempData persists for one redirect cycle รขโ‚ฌโ€ you set it before a redirect and read it after. Useful for flash messages ("Record saved!"). ViewData is a dictionary passed from controller to view within one request. ViewBag is dynamic wrapper over ViewData. TempData uses session or cookies behind the scenes; ViewData and ViewBag don't survive redirects.

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.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

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