---
title: "ViewBag vs ViewData vs TempData in Asp.net MVC"  
description: "In this article describe the concept of the viewbag, viewdata and tempdata in mvc. Here we also describes the example that we understand it."  
author: "Anchal Kesharwani"  
published: 2014-06-25  
updated: 2019-10-11  
canonical: https://www.mindstick.com/articles/1421/viewbag-vs-viewdata-vs-tempdata-in-asp-dot-net-mvc  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# ViewBag vs ViewData vs TempData in Asp.net MVC

In this article [describe the concept](https://www.mindstick.com/forum/158622/describe-the-concept-of-a-divide-by-zero-exception-and-how-to-handle-it) of the viewbag, viewdata and tempdata in mvc. Here we also describes the example that we understand it.

ViewBag

The ViewBag property enables you to dynamically share values from the controller to the view. It is a [dynamic object](https://www.mindstick.com/forum/872/how-to-serialize-list-property-of-a-dynamic-object) which means it has no pre-defined properties. You define the properties you want the ViewBag to have by simply adding them to the property. In the view, you retrieve those values by using same name for the property.

Some important note on ViewBag

- ViewBag is a dynamic property that takes advantage of the new dynamicfeatures in C# 4.0.
- Basically it is a wrapper around the ViewData and also used to pass [data from controller](https://www.mindstick.com/forum/351/how-to-pass-data-from-controller-to-ascx-page) to corresponding view.
- The ViewBag life also lies only during the current request.
- If [redirection](https://www.mindstick.com/forum/436/url-redirection) occurs then viewbag value becomes null.
- It doesn’t required typecasting for getting data.

## Example

```
public ActionResult Index()        {            ViewBag.Message = "Hello MVC ViewBag property.";            ViewBag.Name = "Amit Kumar";            return View();        } 
```

Here we show how to use ViewBage property:

```
@ViewBag.Message Name:@Html.TextBox("Name") 
```

##### ViewData

ViewData is a dictionary which is derived from ViewDataDictionary. The controller can add key/values pairs to the [view data](https://www.mindstick.com/forum/159852/view-data-from-database-using-mvc-entity-framework). The data is then passed to the view when the view is rendered. The view can add to or change the data, which will be sent to the controller when the view is posted as part of a request.

##### Some important note on ViewData

##### · ViewData is a dictionary object that is derived from ViewDataDictionary class.

##### · ViewData is used to pass data from controller to corresponding view.

##### · Its life lies only during the current request.

##### · If redirection occurs then its value becomes null.

##### · It’s required typecasting for getting data and check for null values to avoid error.

##### Example

```
public ActionResult Index()        {             ViewData["Message"] = "Hello for MVC ViewData.";             ViewData["Name"] = "Amit Kumar";                       return View();        } 
```

Here we show how to use it.

```
@ViewData["Message"] @Html.TextBox("Name") 
```

##### TempData

TempData is a dictionary which is derived from TempDataDictionary class. TempData is stored data just like live session for short time. TempData Keep data for the time of [HTTP Request](https://www.mindstick.com/forum/34241/how-to-send-http-request-in-ios-using-nsurlconnection-objective-c) it mean that it hold data between two consecutive requests. TempData help us to transfer [data between controllers](https://www.mindstick.com/forum/161384/how-do-you-share-data-between-controllers-in-angularjs) or between actions. TempData internally use [Session variables](https://www.mindstick.com/forum/267/session-variables). Note that TempData is only work during the current and subsequent request. It is generally used to store one time message. With the help of TempData.Keep() method we can keep value in TempData object after request completion.

##

##### Some important note on TempData

##### · TempData is a dictionary object that is derived from TempDataDictionary class

##### and stored in short lives session.

##### · TempData is used to pass data from current request to subsequent request

##### (means redirecting from one page to another).

##### · Its life is very short and lies only till the target view is fully loaded.

##### · It’s required typecasting for getting data and check for null values to avoid error.

##### · It is used to store only one time messages like error messages, validation

##### messages.

##### Example

```
public ActionResult Index()        {                       TempData["Message"] = "Hello this is my TempData program.";             TempData["Name"] = "Amit Kumar";                       return View();         } 
```

##### Here we show how to use it.

```
@TempData["Message"]       @Html.TextBox("Name")
```

---

Original Source: https://www.mindstick.com/articles/1421/viewbag-vs-viewdata-vs-tempdata-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
