How to store data on asp.net razor page using hidden field?

Ahmed Abd El Aziz 315 Reputation points
2023-07-05T22:03:22.98+00:00

I work on asp.net razor page .i face issue when navigate to page after make submit it reset all values on page

I read more 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 ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,729 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,561 questions
0 comments No comments
{count} votes

Accepted answer
  1. Chen Li - MSFT 1,221 Reputation points
    2023-07-06T03:10:33.18+00:00

    Hi @Ahmed Abd El Aziz,

    Do you want to redirect to another page with parameters? Like this:

    Index.cshtml.cs:

    public class IndexModel : PageModel
    {
        [BindProperty]
        public string username { get; set; }
        [BindProperty]
        public string email { get; set; }
        public IActionResult OnGet()
        {
            username = "Tom";
            email = "Tom@email.com";
            return Page();
        }
    
        public IActionResult OnPost()
        {
            return RedirectToPage("/Privacy", new {username = username, email = email });
        }
    }
    

    Index.cshtml:

    @page
    @model IndexModel
    <div>
        <form method="post">
            <div>
                <input type="hidden" asp-for="username" />
                <input type="hidden" asp-for="email"/>
            </div>
            <div>
                <input type="submit" value="Submit" />
            </div>
        </form>
    </div>
    

    Privacy.cshtml.cs:

    public class PrivacyModel : PageModel
    {
        public void OnGet(string username, string email)
        {
        }
    }
    

    TestResult:

    User's image


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Chen Li


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.