英文:
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?
<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
答案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 .
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论