---
title: "what is the use of TempData in Asp.Net Mvc"  
description: "what is the use of TempData in Asp.Net Mvc"  
author: "Shikhar Arora"  
published: 2019-12-18  
updated: 2019-12-18  
canonical: https://www.mindstick.com/forum/155593/what-is-the-use-of-tempdata-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: [".net", "mvc"]  
reading_time: 2 minutes  

---

# what is the use of TempData in Asp.Net Mvc

How to send [data from controller](https://www.mindstick.com/forum/351/how-to-pass-data-from-controller-to-ascx-page) to [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view)

## Replies

### Reply by Shikhar Arora

TempData in MVC is used to pass [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from Controller to Controller or [Controller to view](https://www.mindstick.com/forum/12918/pass-an-object-from-controller-to-view-with-asp-dot-net) and it is used to pass data from the current request to the next request.

TempData has a short life and it required typecasting. Following is the code scrap of TempData in MVC

```
// Summary:
//     Gets or sets the dictionary for temporary data.
//
// Returns:
//     The dictionary for temporary data.
public TempDataDictionary TempData { get; set; }
```

TempData is a dictionary object that is obtained from the TempDataDictionary class. We will see how we can assign value to TempData in Controller with a simple example for that create one new controller in the application and write code like as shown below

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Controllers
{    public class TestController : Controller
    {
        public ActionResult Index()
        {               TempData["test"] = "hi Tempdata";
                return View();
        }
    } }
```

Now we will create one view to show data for that right click on controller à select add view give the name for that view (Index.cshtml). Open view file and write the following code to show data in the website

```
@{
    ViewBag.Title = "Index"; }
 <h2>Index</h2>
@TempData["test"]
```

When we run the above code we will get output like as shown below

![what is the use of TempData in Asp.Net Mvc](https://www.mindstick.com/mindstickforums/35d73c0f-e36e-4521-aef2-d0345b5bcaf1/images/e41b840c-691c-465c-846f-f1f7a5f6ac91.png)\


---

Original Source: https://www.mindstick.com/forum/155593/what-is-the-use-of-tempdata-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
