title: TempData is not clearing as expected
body: <p>I'm working on an application using ASP.NET 4.0 and MVC 2.0. If it's in any way relevant, I'm using VS2010. </p>
  
  <p>I'm running into complications with TempData. I did not write the original code, but it isn't working correctly and I'm attempting to fix it. I don't have a lot of experience working with TempData and ViewData.</p>
  
  <p>I have an Index action as follows (pseudocode):</p>
  
  <pre><code>public virtual ActionResult Index()
  {
     var vm = new IndexViewModel();
  
     // some code here to set up the ViewModel
  
     if (TempData.ContainsKey("Success"))
        vm.Success = true; 
  
     return View(MVC.Controller.Views.Index, vm);
  }
  </code></pre>
  
  <p>And I have a POST action as follows (pseudocode):</p>
  
  <pre><code>[HttpPost]
  public virtual ActionResult Index(IndexViewModel vm, List&lt;int&gt; formData)
  {
      if (DoSomethingWithData(formData))
      {
         TempData["Success"] = true;
         return RedirectToAction(MVC.Controller.ActionNames.Index);
      }
  
      TempData["Message"] = "Failed to use formData";
  
      return View(MVC.Controller.Views.Index, vm);
  }
  </code></pre>
  
  <p>The view emits a form and prefaces it with a success message if vm.Success is true. It will also emit the message in TempData["Message"] if it is present.</p>
  
  <p>The first time I come to the page I get just the form. I enter INVALID form data and submit it... and I get the form prefaced by the error message as expected. (I know there's poor design here since it doesn't redirect... and you get poor user experience with refresh, etc. but I'm not worried about that yet) This is all great.</p>
  
  <p><strong>The problem manifests</strong> when I use VALID form data. If I submit valid form data, I get the page back prefaced with a success message as expected, but if I refresh the page the success message is still there. Indeed if I go to a completely different part of the site and navigate back, the success message is still there. For some reason after a redirect and read, the tempdata is still there. There has been both a redirect and a read... shouldn't the temp data now be clear?</p>
  
  <p>I'm reasonably certain that the other places I navigate to aren't setting TempData["Success"] for any reason, but to be sure I've navigated to things like Google, and come back directly to the URL for this page, and it still seems as though TempData["Success"] is populated. </p>
  
  <p>It's very clear that either I don't clearly understand how TempData is supposed to function (unsurprising) or something unusual is happening that I simply don't have the experience to see.</p>
  
  <p>Any advice is welcome!</p>
  
  <p>Thanks,
  Dave</p>
  
  <p>[EDIT]</p>
  
  <p>The view doesn't actually emit the form when there's a success message... it only emits the success message.</p>
  
  <p>The view looks more or less like this:</p>
  
  <pre><code>&lt;% if (TempData.ContainsKey("Message")) { %&gt;
     Emit message...
  &lt;% } %&gt;
  
  &lt;% using (Html.BeginForm(MVC.Controller.ActionNames.Index, 
                           MVC.Controller.Name, 
                           FormMethod.Post, 
                           new { id = "form"})) { %&gt;
     &lt;% if (!Model.Success) { %&gt;
        Emit form...
     &lt;% } else { %&gt;
        Emit confirmation message...
     &lt;% } %&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>Francisco pointed me towards something I hadn't considered... but it turns out the constructor for the viewmodel sets Success to false... so it's not something odd with that. I know for sure that TempData["Success"] is still set (rather than something like foolishly reusing a viewmodel with success set to true) because I've stepped through the code and it continually steps into that if statement where it sets vm.success = true, even after a refresh.</p>