My understanding of the TempData attribute in asp.net core is that it marks a model property as needing to persist in value between posts and gets of same page (and possibly wider lifetime than that, but at least that).
My issue is that any property I have marked as TempData and set successfully in OnGetAsync has been reset to null by the time user posts back the form. Why might that be?
Or have I misunderstood what TempData is supposed to do as an attribute? If I have, what's the best way to acheive what I'm trying to do? Pass the phone number to the view and then post it back to OnPostAsync?!?
public class MyPageModel : PageModel
{
[TempData] public string PhoneNumber { get; set; }
public async Task<IActionResult> OnGetAsync(string phoneNumber)
{
PhoneNumber = phoneNumber; //THIS IS WORKING
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
user.PhoneNumber = PhoneNumber; //BUT BY HERE PHONENUMBER is NULL?
}
}
//In Startup.ConfigureServices // I added the CookieTempDataProvider which I did'nt have before, but I believe that CookieTempDataProvider is enabled by default: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.AddSession(options =>
{
//// Set a short timeout for easy testing.
//options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
});
Thoughts?!