1

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?!

4
  • Actually I just noticed 1) It works fine in Firefox 2) It was failing in Google Chrome. In both I had cleared cookies. I wonder if my Ghostery (installed in Chrome) was causing the problem by disallowing a cookie? Or some setting in Chrome? Normallly most websites work just fine so I'd be a bit surprised.
    – ubienewbie
    Commented Aug 24, 2018 at 8:02
  • For my original question though, I'd still be keen to understand: to use TempData attributes successfully to mark model properties as persistent between http calls, what exactly is needed in Startup.ConfigureServices? I could just experiment myself taking lines out and re-running, but since I'm getting local browser weirdness as per previous comment, I'd be keen for guidance on what should be needed.
    – ubienewbie
    Commented Aug 24, 2018 at 8:07
  • Are you sure TempData is what you want to use? learn.microsoft.com/en-us/aspnet/core/fundamentals/… The TempData is only stored until it's read. It sounds like you just need to split your model and view model. Your model is used to pass data to the webserver and the view model is the object that is passed to the view to present the data manipulated. stackoverflow.com/questions/4061440/… Commented Aug 24, 2018 at 9:08
  • @Emma you're probably right that for phone number it seems like a view model type property. I was having same problem with StatusMessage, where i wanted to pass StatusMessage generated by one page to be displayed on the page that the first page then redirected to. And again, [TempData] decorated StatusMessage was fine in one page, blank in the next BUT again issue was cookie / chrome / gremlins related I believe.
    – ubienewbie
    Commented Aug 24, 2018 at 15:02

2 Answers 2

7

OK I finally see what my problem was. I had the CookiePolicyOptions options.CheckConsentNeeded lambda still in place, even though I had taken out the CookieConsentPartial view. So, I assume that the cookies that provided the backing for TempData were not getting set because effectively, I had not consented to them.

For reference, after sorting out the below, I did not need to have ANYTHING TempData or Cookie related in my Configure or ConfigureServices. Because as per Session and app state in ASP.NET Core article:

In ASP.NET Core 2.0 or later, the cookie-based TempData provider is used by default to store TempData in cookies.

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
           // options.CheckConsentNeeded = context => true; // WORKED FINE WHEN I COMMENTED IT OUT.  DID NOT WORK WHEN COMMENTED IN.
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
1
  • Wow, this would've been the very last place I would've expected an issue. Thanks!
    – sheng
    Commented Jun 13, 2019 at 22:04
1

If you want to bind a property you should set attribute to it [BindProperty] and that should work for post actions:

e.g.

 [BindProperty]
 public string PhoneNumber { get; set; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.