I work on razor page asp.net . I face issue my issue values on get reset after click submit button so when click submit button and check values on POST I found it become null
so How to avoid this behavior OR with another meaning how to retain values on get without reset when click submit button
from my debug details as below
1 - when page load i check user id it have value 9595
public void OnGet()
{
string MyUserData = Request.Cookies["myUserData"];
UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize(MyUserData);
string UserID = userAuthResponse.Data.UserID;
}2 - Now I will click submit button so break point hit on post method
public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch)
{
//userAuthResponse.Data.UserID become null
}when check values of userAuthResponse.Data.UserID it become null
so how to preserve or retain value of userAuthResponse.Data.UserID after make get to use it on post method
form view as below :
Submitissue debug details as below :


Amit MohantyRank #112 · 20K reputationPosted Jun 8, 2023, 1:32 AM EDT
In order to retain values between GET and POST requests in Razor Pages, you can make use of hidden fields or session variables.
Use a hidden field in your Razor Page to store the value of userAuthResponse.Data.UserID.
In your POST method, you can retrieve the value of
UserIDfrom the form data:public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch) { string userID = Request.Form["UserID"]; }Alternatively, you can also use session variables to store the value of
UserIDbetween requests.public void OnGet() { string MyUserData = Request.Cookies["myUserData"]; UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize(MyUserData); string UserID = userAuthResponse.Data.UserID; HttpContext.Session.SetString("UserID", UserID); } public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch) { string userID = HttpContext.Session.GetString("UserID"); }Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 8, 2023, 1:06 AM EDT
public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch, string UserID) { // Access the UserID value here // ... }