---
title: "What is the difference between TempData, ViewData, and ViewBag?"  
description: "What is the difference between TempData, ViewData, and ViewBag?"  
author: "ICSM Computer"  
published: 2025-06-12  
updated: 2025-06-12  
canonical: https://www.mindstick.com/interview/34237/what-is-the-difference-between-tempdata-viewdata-and-viewbag  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What is the difference between TempData, ViewData, and ViewBag?

In ASP.NET MVC, `TempData`, `ViewData`, and `ViewBag` are used to **pass data from controllers to views**, but they differ in **lifetime, usage, and behavior**.

## Quick Comparison Table

| Feature | `ViewData` | `ViewBag` | `TempData` |
| --- | --- | --- | --- |
| Type | `Dictionary<string, object>` | Dynamic (wraps `ViewData`) | `TempDataDictionary` (`string` keys) |
| Lifetime | Current request only | Current request only | Survives redirect (one request) |
| Purpose | Pass data to view | Same as `ViewData`, more concise | Pass data across requests (e.g. after redirect) |
| Syntax | `ViewData["Name"]` | `ViewBag.Name` | `TempData["Name"]` |
| Null safety | Prone to key typos | Prone to runtime errors | Same |
| Use case | Any type of view data | Simpler syntax for view data | One-time messages, redirects |

## Detailed Breakdown

### `ViewData`

- Dictionary-like object (`ViewDataDictionary`)
- Available **only during the current HTTP request**
- Requires **type casting** and string keys

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

// View
<h2>@ViewData["Message"]</h2>
```

### `ViewBag`

- Syntactic sugar over `ViewData` using **dynamic**
- Cleaner and avoids casting, but less safe at compile time
- Also **available only during the current request**

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

// View
<h2>@ViewBag.Message</h2>
```

### `TempData`

- Stores data **until it is read** (or next request ends)
- **Survives a redirect** (`RedirectToAction`)
- Backed by `Session` — so session must be enabled

```cs
// Controller
TempData["SuccessMessage"] = "Profile updated successfully";
return RedirectToAction("Index");

// In redirected view
@if (TempData["SuccessMessage"] != null)
{
    <div>@TempData["SuccessMessage"]</div>
}
```

## When to Use What?

| Scenario | Use |
| --- | --- |
| Pass data to view within same request | `ViewBag` or `ViewData` |
| Share data between controller and view | `ViewBag` or `ViewData` |
| Pass one-time messages (after redirect) | `TempData` |
| Maintain small data across requests | `TempData` |
| Store model or large objects | Use `ViewModel` instead |

## Tip: Reuse ViewBag/ViewData with Partial Views

Both `ViewBag` and `ViewData` are passed automatically to partial views:

```cs
@Html.Partial("_MyPartial", model) // gets the ViewData and ViewBag
```

## Pitfalls

- **ViewBag/ViewData** are **not type-safe** – you can mistype the key or use wrong casting.
- **TempData** gets cleared after one request unless you use `TempData.Keep()`.

## Answers

### Answer by ICSM Computer

In ASP.NET MVC, `TempData`, `ViewData`, and `ViewBag` are used to **pass data from controllers to views**, but they differ in **lifetime, usage, and behavior**.

## Quick Comparison Table

| Feature | `ViewData` | `ViewBag` | `TempData` |
| --- | --- | --- | --- |
| Type | `Dictionary<string, object>` | Dynamic (wraps `ViewData`) | `TempDataDictionary` (`string` keys) |
| Lifetime | Current request only | Current request only | Survives redirect (one request) |
| Purpose | Pass data to view | Same as `ViewData`, more concise | Pass data across requests (e.g. after redirect) |
| Syntax | `ViewData["Name"]` | `ViewBag.Name` | `TempData["Name"]` |
| Null safety | Prone to key typos | Prone to runtime errors | Same |
| Use case | Any type of view data | Simpler syntax for view data | One-time messages, redirects |

## Detailed Breakdown

### `ViewData`

- Dictionary-like object (`ViewDataDictionary`)
- Available **only during the current HTTP request**
- Requires **type casting** and string keys

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

// View
<h2>@ViewData["Message"]</h2>
```

### `ViewBag`

- Syntactic sugar over `ViewData` using **dynamic**
- Cleaner and avoids casting, but less safe at compile time
- Also **available only during the current request**

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

// View
<h2>@ViewBag.Message</h2>
```

### `TempData`

- Stores data **until it is read** (or next request ends)
- **Survives a redirect** (`RedirectToAction`)
- Backed by `Session` — so session must be enabled

```cs
// Controller
TempData["SuccessMessage"] = "Profile updated successfully";
return RedirectToAction("Index");

// In redirected view
@if (TempData["SuccessMessage"] != null)
{
    <div>@TempData["SuccessMessage"]</div>
}
```

## When to Use What?

| Scenario | Use |
| --- | --- |
| Pass data to view within same request | `ViewBag` or `ViewData` |
| Share data between controller and view | `ViewBag` or `ViewData` |
| Pass one-time messages (after redirect) | `TempData` |
| Maintain small data across requests | `TempData` |
| Store model or large objects | Use `ViewModel` instead |

## Tip: Reuse ViewBag/ViewData with Partial Views

Both `ViewBag` and `ViewData` are passed automatically to partial views:

```cs
@Html.Partial("_MyPartial", model) // gets the ViewData and ViewBag
```

## Pitfalls

- **ViewBag/ViewData** are **not type-safe** – you can mistype the key or use wrong casting.
- **TempData** gets cleared after one request unless you use `TempData.Keep()`.


---

Original Source: https://www.mindstick.com/interview/34237/what-is-the-difference-between-tempdata-viewdata-and-viewbag

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
