What am I missing that causes [TempData] decorated property not to retain value from get to post?

半世苍凉 提交于 2020-01-25 00:23:12

问题


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://docs.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?!


回答1:


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;
        });



回答2:


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; }


来源:https://stackoverflow.com/questions/51995324/what-am-i-missing-that-causes-tempdata-decorated-property-not-to-retain-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!