[英]Asp.Net-Core with Razor Pages Bind hidden input
In my java script file I am setting the value of a hidden input like this:在我的 java 脚本文件中,我正在设置隐藏输入的值,如下所示:
document.getElementById("myVal").value = 11;
In my html:在我的 html 中:
<input asp-for="Value" id="myVal" />
In my Asp.Net-Core Web Application with Razor Pages I am trying to bind this property so I can take the value:在我的带有 Razor 页面的 Asp.Net-Core Web 应用程序中,我试图绑定此属性,以便获取值:
public class IndexModel : PageModel
{
[HiddenInput] //I also tried to add [BindProperty] after
public int Value { get; set; }
public void OnGet()
{
Debug.WriteLine(Value);
}
}
EDIT编辑
function redirect() {
document.getElementById("myVal").value = 11;
}
This function is called when I click a button, and that is when the value is being set to 11.这个 function 在我单击按钮时被调用,也就是当值设置为 11 时。
What I want is that when this button is clicked, I can save the value myVal so that I can use it in my Razor Page.我想要的是,当单击此按钮时,我可以保存值 myVal 以便我可以在我的 Razor 页面中使用它。
The problem is that the value of the field will always be 0 (even when the field is not hidden).问题是该字段的值将始终为 0(即使该字段未隐藏)。 How can I set the value for the hidden field?如何设置隐藏字段的值?
This work for me这对我有用
i define post method我定义 post 方法
[BindProperty] [HiddenInput] //I also tried to add [BindProperty] after public int Value { get; set; } public void OnGet() { } public void OnPost() { int a = Value; }
*in page *在页面中
<form method="post" asp-action="Index">
<input asp-for="Value" id="myVal" />
<input type="submit" value="send" />
</form>
<script>
document.getElementById("myVal").value = 11;
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.