How to persist and retain values of username after make post submit without reset value using Hidden Fields?

Ahmed Abd El Aziz 315 Reputation points
2023-07-06T11:45:29.7633333+00:00

I work on asp.net core razor page . I need to retain and save data on the page after submit

page because i check value of Username after submit it not retain and it become null

so I need to retain the value of user name without reset using hidden fields

so How to do that please

<form method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" value="@Model.Username" />

    <input type="submit" value="Submit" />
</form>

and on page model what I do to retain value of user name using hidden fields

public class TestFormModel : PageModel
{
    [BindProperty]
    public string Username { get; set; }


    public void OnGet()
    {
       //what I write here to retain value of user name using hidden fields
    }

    public IActionResult OnPost()
    {
      //what I write here to retain value of user name using hidden fields
    }

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-07-06T12:57:06.84+00:00

    Allow the bind property to support Get as well a POST.

    [BindProperty(SupportsGet = true)]
    public string Username { get; set; }
    

    With an HTTP GET, data is sent in the URL, while a POST is submitted in the HTTP body. See any beginning level HTTP tutorial.

    ?Username=MyUsername

    Typically a link is used to submit data in a URL...

    <div>
        <a asp-route-username="@Model.Username">My Link</a>
    </div>
    

    ...but a form can be used by setting the form method property to "get".

    <form method="get">
    

    Keep in mind, the markup you shared above has errors and does not compile which tells us the shared code is not the actual code you are running or testing. This makes providing an accurate solution difficult because we have no idea what you're doing.

    @page
    @model RazorPagesDemo.Pages.Forms.SubmitValueModel
    
    <form method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" value="@Model.Username" />
    
        <input type="submit" value="Submit" />
    </form>
    <div>
        <a asp-route-username="@Model.Username">My Link</a>
    </div>
    
        public class SubmitValueModel : PageModel
        {
    
            public void OnGet()
            {
                //what I write here to retain value of user name using hidden fields
            }
    
            public IActionResult OnPost()
            {
                //what I write here to retain value of user name using hidden fields
                return Page();
            }
    
            [BindProperty(SupportsGet = true)]
            public string Username { get; set; } = "Some Value";
        }
    

    I strongly recommend going through a few tutorials to learn standard Razor Pages programming patterns. This HTTP GET scenario is cover in step 6 of the Razor Page tutorials from this site. Plus it seems you are not familiar with standard Razor Page constructs like tag helper which are intended to make developing web pages simple. However, understanding basic HTTP and HTML is required.

    https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/search?view=aspnetcore-7.0

    If you need to persist data across requests as detailed in your most recent post, then I think you'll be interested in reading about ASP.NET Core State Management. There are several option that you get to choose from depending on your needs.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-7.0

    0 comments No comments

  2. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-07-06T15:21:34.0466667+00:00

    be aware that users can change the data in hidden fields or passed via query string. so any secure data like username should be encrypted.

    in general it is a poor design to use hidden fields to round trip data. typically a data element like user name is stored in an authentication cookie.


  3. AgaveJoe 30,126 Reputation points
    2023-07-06T18:45:47.3433333+00:00

    can you please provide full sample from your side as best practice to store value between get and post without reset value I need it to form type post also if hidden fiels poor design provide me sample for any way best practice

    If this is a security question where you need to know the identity of the user and not a general HTTP question then see the authentication cookie documentation.

    Use cookie authentication without ASP.NET Core Identity

    0 comments No comments

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.