ViewData vs ViewBag vs TempData in ASP.NET Core MVC

After understanding the importance of state management in ASP.NET Core MVC, the next logical step is to explore the most commonly used techniques for passing data between controllers and views.

These techniques — ViewData, ViewBag, and TempData — are fundamental for handling short-lived data in MVC applications.

Although they may seem similar at first, they serve different purposes and are used in different scenarios.


Why Do We Need ViewData, ViewBag, and TempData?

In a typical MVC application, the controller processes the request and sends data to the view.

But how exactly is this data transferred?

This is where these three mechanisms come into play.

// Controller
string message = "Welcome to DotNetFullStack";

// How to send this to View?
ViewData
ViewBag
TempData

Each of these provides a way to pass data — but with different lifetimes and use cases.


Why These Concepts Matter

Let’s take a very simple example.

You submit a form to create a user, and after saving, you redirect to another page.

Now you want to show a success message like:

User created successfully

The question is — how will you pass this message after redirect?

This is not just a coding problem — it’s a request lifecycle problem.

And this is exactly what these three concepts solve.


Quick Overview (High-Level Understanding)

Feature ViewData ViewBag TempData
Type Dictionary Dynamic Object Dictionary
Lifetime Same Request Same Request Next Request
Type Safety No No No
Best Use Case Pass data to View Cleaner syntax Redirect scenarios

What is ViewData?

ViewData is a dictionary-based object used to pass data from a controller to a view within the same request.

// Controller
ViewData["Message"] = "Hello from ViewData";

// View
@ViewData["Message"]

Type Casting Requirement

// Controller
ViewData["Count"] = 10;

// View
@((int)ViewData["Count"])

Key Characteristics

  • Works only for current request
  • Uses key-value dictionary
  • Requires type casting

What is ViewBag?

ViewBag is a dynamic wrapper around ViewData that provides a cleaner syntax.

// Controller
ViewBag.Message = "Hello from ViewBag";

// View
@ViewBag.Message

Behind the Scenes

ViewBag internally uses ViewData.

// Equivalent internally
ViewData["Message"] = ViewBag.Message;

Key Characteristics

  • No type casting required
  • Dynamic in nature
  • Cleaner syntax than ViewData

ViewData vs ViewBag (Important Insight)

Both are used for the same purpose — passing data from controller to view — but differ in syntax and usability.

// ViewData
ViewData["Name"] = "John";

// ViewBag
ViewBag.Name = "John";

ViewBag is generally preferred for readability.


What is TempData?

TempData is used to pass data between two requests, typically after a redirect.

// Controller
TempData["SuccessMessage"] = "Data saved successfully";
return RedirectToAction("Index");

// Next Request (Index View)
@TempData["SuccessMessage"]

Key Characteristics

  • Available for next request only
  • Used after redirects
  • Stored using session internally

TempData Peek and Keep (Very Important)

By default, TempData is designed to store data only until it is read once.

This means as soon as you access TempData in a view or controller, it gets marked for deletion.

// First read
@TempData["Message"]

// Second read
@TempData["Message"] (null)

This behavior is useful in most cases, but sometimes you may want to read the value without removing it, or keep it for another request.

This is where Peek() and Keep() come into play.


TempData.Peek()

Peek() allows you to read the value without marking it for deletion.

This means the data will still be available for the next request.

// Controller or View
var message = TempData.Peek("Message");

// Value is still available after this

When to Use Peek()

  • When you need to display the same TempData multiple times
  • When you don’t want TempData to be removed after reading

TempData.Keep()

Keep() is used to retain TempData values after they have been read.

Normally, once you read TempData, it is marked for deletion — but Keep() prevents that.

// Read TempData
var message = TempData["Message"];

// Preserve it for next request
TempData.Keep("Message");

Keep All Values

TempData.Keep();

When to Use Keep()

  • When TempData is already accessed but needs to persist
  • When passing data across multiple redirects

Peek vs Keep (Key Difference)

Feature Peek() Keep()
Purpose Read without removing Retain after reading
When used Before reading After reading
Affects deletion Prevents marking for deletion Reverses deletion marking

Simple Flow Understanding

// Normal behavior
TempData["Msg"] → Read → Deleted

// Using Peek
TempData.Peek("Msg") → Not deleted

// Using Keep
TempData["Msg"] → Read → Keep() → Not deleted

Real-World Scenario

Imagine you are showing a success message after a redirect, but the page reloads again.

Without Peek or Keep, the message will disappear.

Using Peek ensures the message stays available without being removed immediately.

  • Peek() → Read without deleting
  • Keep() → Preserve after reading
  • Both are used to control TempData lifecycle

Understanding Peek and Keep is important for handling multi-step workflows and redirect-based flows in real applications.

Consider a form submission scenario:

// POST Action
TempData["Message"] = "User Created Successfully";
return RedirectToAction("Index");

Without TempData, the message would be lost after redirect.


Lifecycle Understanding

// Same Request
ViewData
ViewBag

// Next Request
TempData

This is the core difference you must remember.


When to Use What?

Scenario Best Choice
Passing data to View ViewBag / ViewData
Clean syntax needed ViewBag
Redirect scenario TempData
Temporary message TempData

Common Mistakes Developers Make

  • Using ViewData for redirect scenarios
  • Overusing ViewBag instead of strongly typed models
  • Forgetting TempData clears after read

Summary

ViewData, ViewBag, and TempData are essential tools for passing data in ASP.NET Core MVC applications.

While ViewData and ViewBag are used within the same request, TempData is designed for passing data across requests, especially after redirects.

Choosing the right one depends on the scenario, lifecycle, and application design.

In the next article, we will explore Session in ASP.NET Core MVC and understand how to store data across multiple requests.