I work on asp.net razor page .i face issue my issue is when navigate to page after make submit it will reset all values on page
I read more articles related to the issue will solved by using hidden fields
so suppose I have asp.net razor page have username and email
and i need store fields of username and email on hidden fields to be available after submit post
so can you provide to me sample to store username and email on hidden fields so prevent value from reset after submit and can navigate to other pages
so Can you please provide to me sample please to hidden fields store username and email?

Lokesh VarmanRank #374 · 6.8K reputationPosted Jul 5, 2023, 10:16 PM EDT
First, you'll need to add the hidden fields within the
tags of your Razor page. These hidden fields should have unique IDs and names, which will be used to identify them. Here's an example of how you can include the hidden fields in your form:In the above code, the
usernameHiddenandemailHiddenfields are hidden inputs that store the values of@Model.Usernameand@Model.Email, respectively.Next, you'll need to handle the form submission in the associated .cshtml.cs file. In the
OnPostmethod, you can retrieve the values from the hidden fields and update the model properties accordingly. Here's an example:public class MyPageModel : PageModel { // Existing model properties public string Username { get; set; } public string Email { get; set; } public IActionResult OnPost() { // Retrieve the values from the hidden fields string username = Request.Form["usernameHidden"]; string email = Request.Form["emailHidden"]; // Update the model properties Username = username; Email = email; return RedirectToPage("OtherPage"); } }In the
OnPostmethod, you access the values of the hidden fields usingRequest.Form["fieldName"]. Then, you assign those values to the corresponding model properties (UsernameandEmailin this case). After that, you can redirect to another page usingRedirectToPage("OtherPage")or perform any other necessary processing.By following these steps, the values of the username and email fields will be stored in the hidden fields, ensuring they are preserved even after the form submission and navigation to other pages.
ahmed salahRank #2,240 · 628 reputationPosted Jul 6, 2023, 3:19 AM EDT
my input type is text so how to save it ohidden fields please
these two fields user name and email on my form is ready for user to input data
so How to save data of input user text on hidden field please
fff
Amit MohantyRank #112 · 20K reputationPosted Jul 6, 2023, 5:46 AM EDT
Check this
@page @model DMS.Website.Pages.Home.TestFormModel @{ } Username: Email: document.getElementById("username").addEventListener("input", function () { document.getElementById("hiddenUsername").value = this.value; }); document.getElementById("email").addEventListener("input", function () { document.getElementById("hiddenEmail").value = this.value; });public class TestFormModel : PageModel { [BindProperty] public string Username { get; set; } [BindProperty] public string Email { get; set; } public void OnGet() { // Your code to populate the model properties on page load } public IActionResult OnPost() { // Access the values from the hidden fields string hiddenUsername = Request.Form["hiddenUsername"]; string hiddenEmail = Request.Form["hiddenEmail"]; // Perform any necessary processing with the values // Redirect to another page return Page(); } }