TempData not working for second request in MVC4

I had never seen this before and was at a standstill. I have the following sequence of controllers:

/// <summary> /// Helper method to store offerId to TempData /// </summary> /// <param name="offerId"></param> private void StoreOfferInTempData(string offerId) { if (TempData.ContainsKey(SelectedOfferKey)) TempData.Remove(SelectedOfferKey); TempData.Add(SelectedOfferKey, offerId); } [HttpPost] [AllowAnonymous] public virtual ActionResult Step1(MyViewModel model) { if (ModelState.IsValid) { StoreOfferInTempData(model.SelectedOfferId); return RedirectToAction(MVC.Subscription.Register()); } MySecondViewModel model2 = new MySecondViewModel { OfferId = model.SelectedOfferId }; return View(model2); } [HttpGet] [AllowAnonymous] public virtual ActionResult Register() { string offerId = TempData[SelectedOfferKey] as string; //we get a valid value here ... error handling content elided ... RegisterViewModel model = new RegisterViewModel { OfferId = offerId }; return View(model); } [HttpPost] [AllowAnonymous] public virtual ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { CreateCustomerResult result = CustomerService.CreateAccount(model.Email, model.NewPassword); if (result.Success) { ... content elided; storing customer to Session ... MyMembershipProvider.PersistUserCookie(result.Principal, true); //need to store our selected OfferId again for use by the next step StoreOfferInTempData(model.OfferId); return RedirectToAction(MVC.Subscription.Payment()); } model.ErrorMessage = result.ErrorMessage; } return View(model); } [HttpGet] public ActionResult Payment() { string offerId = TempData[SelectedOfferKey] as string; //this is null??? ... content elided ... return View(model); } 

The first round of storage in TempData behaves as expected. The value is present in the subsequent HttpGet method and is marked for deletion, so it is no longer there when I add it again. However, in the third HttpGet method, it returns null.

I tried using different keys for each round unchanged. I can assure you that I never check TempData, except for the displayed ones, so I donโ€™t see the value being somehow marked for deletion. In addition, it doesnโ€™t work in the payment method whether it has the [AllowAnonymous] attribute or not (therefore, not because of any http switch to https or something like that.

It seems that it should be something very simple, but my searches have not changed anything. Any help was greatly appreciated.

UPDATE: During further checks, it seems that my whole context at this step was closed. We use IoC in controllers, but none of the elements created by IoC exist. The mystery deepens.

+4
source share
3 answers

Yeah! Well, this is not complicated enough, and I hope this helps someone else. It turns out that I forgot to run the T4MVC.tt file after creating the Payment () actions, so RedirectToAction, which takes the MVC.Subscription.Payment () action, did not create the correct control over the controller. I do not understand all the magic here, but if you come across this and use T4MVC.tt, make sure you run it!

Comments on why this would be welcome.

+1
source

TempData is only until it returns to its original state or processes the next request (which ever comes first). You should not rely on TempData if you go to two (or three) queries. Use a session or database instead.

The purpose of TempData is to transfer information between requests that are not perpetuated until you clear it (what sessions are for).

+3
source

Use TempData.Keep("key") to save values โ€‹โ€‹between multiple post-spins

0
source

All Articles