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