Search code examples
razorasp.net-core-mvcrazor-pages

How to retain values of Address after make post submit without reset value?


I work on an ASP.NET Core razor page. I need to retain value of address on the page after submit, because I check value of Address after submit - it is not retained, and becomes null.

I need to retain the value of the Address without resetting, using any way without using session or cookies . How to do that please?

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

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

In my page model, what do I to do to retain the value of Address without using session or cookies?

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

    public void OnGet()
    {
       // what do I need to write here to retain 
       // value of Address without using session or cookies
    }

    public IActionResult OnPost()
    {
       // what do I need to write here to retain 
       // value of Address  using session or cookies
    }
}

Updated post

I can't solve issue on Address field using Hidden fields what I try until now

step 1

<input type="text" id="Address" name="Address" value="@Model.Address" />
<input type="hidden" id="hiddenAddress" name="hiddenAddress" value="@Model.Address" />

Public void OnPost(string Address)
{
}

I check address value it have value null

although I enter value on it so please how to prevent post value from

reset on post using hidden field please

Updated post

How to assign value of Hidden Address field to value of Address


Solution

  • Thanks for support

    I solved my issue of pass data between get and post without reset value and

    become null by using hidden field

    and use java script add event listener to retain value of hidden field Address

    to address without reset it .

    <form method="post">
        <label for="Address">Address:</label>
        <input type="text" id="Address" name="Address" value="@Model.Address" />
    
    
        <input type="hidden" id="hiddenAddress" name="hiddenAddress" value="@Model.Address" />
     
    
    
        <input type="submit" value="Submit" />
    </form>
    <script>
        document.getElementById("Address").addEventListener("input", function () {
            document.getElementById("hiddenAddress").value = this.value;
        });
    </script>