---
title: "Discuss the usage of ViewBag, ViewData, and TempData in ASP.NET MVC."  
description: "Discuss the usage of ViewBag, ViewData, and TempData in ASP.NET MVC."  
author: "Revati S Misra"  
published: 2023-06-01  
updated: 2023-06-02  
canonical: https://www.mindstick.com/forum/158589/discuss-the-usage-of-viewbag-viewdata-and-tempdata-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc", "mvc"]  
reading_time: 3 minutes  

---

# Discuss the usage of ViewBag, ViewData, and TempData in ASP.NET MVC.

[Discuss the usage](https://www.mindstick.com/forum/158593/discuss-the-usage-of-validation-in-asp-dot-net-mvc-and-the-available-validation-attributes) of [ViewBag](https://www.mindstick.com/forum/155739/define-viewbag-in-mvc), [ViewData](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username), and [TempData](https://www.mindstick.com/forum/155741/define-tempdata-in-mvc) in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc).

## Replies

### Reply by Aryan Kumar

ViewBag, ViewData, and TempData are all objects that can be used to pass data from a controller to a view in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) MVC. However, they differ in a few key ways:

- **ViewBag:** ViewBag is a dynamic object that can be used to pass any type of data to a view. It is not strongly typed, so you do not need to specify the data type when you assign a value to it.
- **ViewData:** ViewData is a dictionary object that can be used to pass data to a view. It is strongly typed, so you need to specify the data type when you assign a value to it.
- **TempData:** TempData is a temporary storage mechanism that can be used to pass data between two consecutive requests. It is also strongly typed, so you need to specify the data type when you assign a value to it.

In general, ViewBag should be used for simple data types and when you do not need to keep the data between two consecutive requests. ViewData should be used for complex data types and when you need to keep the data between two consecutive requests. TempData should be used for data that needs to be accessed by multiple controllers or views.

Here are some examples of how ViewBag, ViewData, and TempData can be used:

- **Passing a simple data type:**

Code snippet

```plaintext
public ActionResult Index()
{
    ViewBag.Name = "John Doe";
    return View();
}
```

In this example, the Name property of the ViewBag object is assigned the value "John Doe". This value will be available to the view as a dynamic property.

- **Passing a complex data type:**

Code snippet

```plaintext
public ActionResult Index()
{
    var user = new User
    {
        Name = "John Doe",
        Age = 30,
        Gender = "Male"
    };

    ViewData["User"] = user;
    return View();
}
```

In this example, the User object is assigned to the User property of the ViewData dictionary. This value will be available to the view as a strongly typed property.

- **Passing data between two consecutive requests:**

Code snippet

```plaintext
public ActionResult Index()
{
    TempData["Message"] = "Welcome to my website!";
    return RedirectToAction("Home");
}

public ActionResult Home()
{
    var message = TempData["Message"];
    // Do something with the message
    return View();
}
```

In this example, the Message property of the TempData dictionary is assigned the value "Welcome to my website!". This value will be available to the Home action method as a strongly typed property.

ViewBag, ViewData, and TempData are all powerful tools that can be used to pass data between a controller and a view in ASP.NET MVC. However, it is important to understand the differences between them so that you can use them effectively.


---

Original Source: https://www.mindstick.com/forum/158589/discuss-the-usage-of-viewbag-viewdata-and-tempdata-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
