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();
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.