.NET Core 5 [TempData] Annotation

Fırat Ataş 1 Reputation point
2022-01-18T09:05:38.863+00:00

Hi,

.Net Core 5 razor pages, using TempData, direct or annotation gives different results;

  public class TestModel : PageModel  
    {  
        [TempData]  
        public string Test { get; set; }  
  
        public async Task<IActionResult> OnPostAsync()  
        {  
            TempData["direct"] = "Test-Direct";  
            Test = "Test-Annotation";  
  
            return Page();  
        }  
    }  

.chtml Page;

@if (TempData["direct"] != null)  
{  
@TempData["direct"]  
}  
@if (Model.Test != null)  
{  
@Model.Test  
}  

After return Page();
166011-image.png

And then resend browser link(like RedirectToPage)
165939-image.png

What is the problem?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,309 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,546 Reputation points
    2022-01-18T15:42:56.01+00:00

    Works as expected.

    You only set the values on a post. A redirect is a get. So if you do a get after the post, only the attribute code will be processed. At the attribute value is set in the get, that value is copied.

    1 person found this answer helpful.
    0 comments No comments