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.
source share