TempData does not clear as expected

I am working on an application using ASP.NET 4.0 and MVC 2.0. If this is relevant in any way, I am using VS2010.

I'm having difficulty with TempData. I did not write the original code, but it does not work correctly, and I'm trying to fix it. I do not have much experience with TempData and ViewData.

I have an index action as follows (pseudocode):

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); } 

And I have a POST action as follows (pseudocode):

 [HttpPost] public virtual ActionResult Index(IndexViewModel vm, List<int> 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); } 

The view emits a form and predicts it with a successful message if vm.Success is true. It also throws a message in TempData ["Message"], if present.

The first time I come to the page, I get only the form. I enter the INVALID data of the form and submit it ... and I get the form preceded by the error message, as expected. (I know that the design is bad here, as it does not redirect ... and you get a bad experience with updating, etc., but I'm not worried about that yet). This is all great.

The problem manifests itself when I use VALID form data. If I submit valid form data, I get a return page with a prefix message of success, 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 go back, the success message is still there. For some reason, after redirecting and reading, tempdata still exists. There was both redirection and reading ... wouldn't the data of temporary data be clear?

Iโ€™m sure that the other places Iโ€™m moving to do not set TempData [โ€œSuccessโ€] for any reason, but Iโ€™m sure that I have switched to things like Google and will return directly to the URL for this page , and it still seems that TempData ["Success"] is populated.

It is clear that either I donโ€™t understand how TempData should function (not surprisingly), or something unusual happens that I just donโ€™t see the experience.

Any advice is appreciated!

Thanks Dave

[EDIT]

In reality, the view does not give out the form when a success message appears ... it only emits a success message.

The view looks something like this:

 <% if (TempData.ContainsKey("Message")) { %> Emit message... <% } %> <% using (Html.BeginForm(MVC.Controller.ActionNames.Index, MVC.Controller.Name, FormMethod.Post, new { id = "form"})) { %> <% if (!Model.Success) { %> Emit form... <% } else { %> Emit confirmation message... <% } %> <% } %> 

Francisco pointed out to me that I did not consider ... but it turns out that the constructor for the viewmodel sets Success to false ... so this is not something strange with that. I know for sure that TempData ["Success"] is still installed (and not something like stupidly reusing the view model with success set to true), because I went through the code and constantly goes into this if statement, where he installs vm. success = true, even after updating.

+7
source share
2 answers

Just add this as I said in my comment. I suggest to do

 if (TempData["Success"] != null) vm.Success = true; 

Instead

 if (TempData.ContainsKey("Success")) vm.Success = true; 

... therefore it is considered reading TempData. Glad it worked. Relationship

+7
source

Microsoft has made changes to the behavior of TempData, which we need to know about in MVC 2 and 3. TempData is no longer completely cleared at the end of the controller action cycle. TempData now (automatically and without your changing anything) is saved on other pages. TempData keys are now cleared only if they have been read. In addition, if you use RedirectResult or RedirectToRouteResult, they will persist even if they are read.

Read more: warning-mvc-nets-tempdata-now-persists-across-screens

+10
source

All Articles