如何在提交后保留地址的值而不重置数值?

huangapple go评论61阅读模式
英文:

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

问题

我在一个ASP.NET Core Razor页面上工作。我需要在提交后保留地址的值,因为我在提交后检查地址的值 - 它没有保留,变成了null。

我需要保留地址的值而不重置,使用任何方式但不使用会话或cookie。请问如何做到这一点?

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

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

在我的页面模型中,我需要做什么来保留地址的值,而不使用会话或cookie?

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

    public void OnGet()
    {
        // 我需要在这里写什么来保留地址的值,而不使用会话或cookie
    }

    public IActionResult OnPost()
    {
        // 我需要在这里写什么来保留地址的值,使用会话或cookie
    }
}

更新的帖子

我无法使用隐藏字段解决地址字段的问题
到目前为止,我尝试了以下内容

步骤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)
{
}

我检查了地址的值,它的值为null

尽管我在它上面输入了值,请问如何防止使用隐藏字段时重置提交值?

更新的帖子

如何将Hidden Address字段的值分配给Address的值

英文:

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?

&lt;form method=&quot;post&quot;&gt;
    &lt;label for=&quot;Address&quot;&gt;Address:&lt;/label&gt;
    &lt;input type=&quot;text&quot; id=&quot;Address&quot; name=&quot;Address&quot; value=&quot;@Model.Address&quot; /&gt;

    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;

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

&lt;input type=&quot;text&quot; id=&quot;Address&quot; name=&quot;Address&quot; value=&quot;@Model.Address&quot; /&gt;
&lt;input type=&quot;hidden&quot; id=&quot;hiddenAddress&quot; name=&quot;hiddenAddress&quot; value=&quot;@Model.Address&quot; /&gt;

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

答案1

得分: 0

感谢支持。

我解决了在GET和POST之间传递数据并避免重置数值的问题,通过使用隐藏字段并使用JavaScript添加事件监听器来保留隐藏字段"Address"的值,而不重置它。

<form method="post">
    <label for="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="提交" />
</form>
<script>
    document.getElementById("Address").addEventListener("input", function () {
        document.getElementById("hiddenAddress").value = this.value;
    });
</script>
英文:

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 .

&lt;form method=&quot;post&quot;&gt;
    &lt;label for=&quot;Address&quot;&gt;Address:&lt;/label&gt;
    &lt;input type=&quot;text&quot; id=&quot;Address&quot; name=&quot;Address&quot; value=&quot;@Model.Address&quot; /&gt;


    &lt;input type=&quot;hidden&quot; id=&quot;hiddenAddress&quot; name=&quot;hiddenAddress&quot; value=&quot;@Model.Address&quot; /&gt;
 


    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
&lt;/form&gt;
&lt;script&gt;
    document.getElementById(&quot;Address&quot;).addEventListener(&quot;input&quot;, function () {
        document.getElementById(&quot;hiddenAddress&quot;).value = this.value;
    });
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年7月6日 19:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定