I am using TempData for storing ViewModel that is needed on a in the Register method.
My code snippet for RedirectToAction - shows adding ViewModel to TempData
TempData.Put("Email", verificationResponse.Email);
TempData.Put("Phone",verificationResponse.Phone);
RegisterViewModel rvm = new RegisterViewModel
{
PolicyID = vm.PolicyID,
FirstName = vm.FirstName,
LastName = vm.LastName,
DOBMM = vm.DOBMM,
DOBDD = vm.DOBDD,
DOBYYYY = vm.DOBYYYY,
ZipCode = vm.ZipCode,
ReturnUrl = vm.ReturnUrl,
EnableRegister = true,
Email = verificationResponse.Email,
Username = verificationResponse.Email
};
TempData.Put("rvm", rvm);
return RedirectToAction("Register", "Auth");
But when I accessed TempData in Register it was null.
[HttpGet]
public IActionResult Register()
{
RegisterViewModel rvm = TempData.Get<RegisterViewModel>("rvm");
TempData.Keep("rvm");
return View(rvm);
}
Ensure that you do not store empty strings in TempData!!! You might think it's only a problem for one key/value pair. It's not! The entire TempData is null.
My problem was this line
TempData.Put("Email", verificationResponse.Email);The verificationResponse.Email was an empty string and it caused the TempData to be null in the Register method.Spent too much time looking at this bug.