---
title: "Despite no request tempdata[] getting wiped"  
description: "Despite no request tempdata[] getting wiped"  
author: "Anonymous User"  
published: 2014-11-21  
updated: 2014-11-21  
canonical: https://www.mindstick.com/forum/12670/despite-no-request-tempdata-getting-wiped  
category: "asp.net"  
tags: ["c#", "asp.net mvc", "mvc4", "web api"]  
reading_time: 2 minutes  

---

# Despite no request tempdata[] getting wiped

[TempData](https://www.mindstick.com/forum/155741/define-tempdata-in-mvc) is supposed to persist for a subsequent [request](https://www.mindstick.com/blog/255/post-get-and-request-function-in-php). However when I [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) the TempData in a subsequent [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) I get a null. Why is TempData[] getting wiped off. Here are my two controllers. I am calling them in succession. So TempData[] should persist from one request to the next. Here is [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) from controller 1:

```
[HttpGet]public ActionResult EditCampaign(int? Id){     ViewBag.Banner = bannerText.Replace(" ", "&nbsp;");    // This is passed in case we need to add a new product or edit one. Then we'll    // need the campaign id for reference purposes.    TempData["campaign_id"] = Id;    TempData["campaignBanner"] = bannerText.Replace(" ", "&nbsp;");    ViewBag.StartDate = debugger.startDate.ToShortDateString();    ViewBag.EndDate = debugger.endDate.ToShortDateString();    int Id1 = Convert.ToInt32(TempData["campaign_id"]);    int convertedId1 = Id1Here is where I try to get the value in Controller 2:[HttpPost]public ActionResult EditCampaign(CampaignDTO camps){    // Do this up top. i.e. get the campaign ID    camps.Id = Convert.ToInt32(TempData["campaign_id"]);    string success = "";
```

I suspect that assigning the [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) of TempData in the penultimate line of controller 1 might be wiping off the data. If thats the case that is something new and not documented. Please help.

## Replies

### Reply by Jayden Bell

Reading the data is enough to remove it. Here's the relevant source:

```
public object this[string key]{    get    {        object value;        if (TryGetValue(key, out value))        {            _initialKeys.Remove(key);            return value;        }        return null;    }    set    {        _data[key] = value;        _initialKeys.Add(key);    }}
```

So in controller 1, when you say:

int Id1 = Convert.ToInt32(TempData["campaign_id"]);

you are actually removing the data you just inserted.

There are many ways to work around this, but if you must read it from TempData, use Peek() instead.

I hope you this will help you


---

Original Source: https://www.mindstick.com/forum/12670/despite-no-request-tempdata-getting-wiped

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
