为什么属性从视图到代码页面无效
我有这样的东西(简短的)支出
。cshtml.cs:
public class Expenditures: PageModel {
[BindProperty(SupportsGet = true)] public string PersonnelNo {get;set;}
public async Task OnGetAsync() { //some code
}
public async Task < IActionResult > OnPostAsync() {
return Page();
}
}
and earvenditures.cshtml,
@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.Expenditures
@{
ViewData["Title"] = "Expenditures";
}
<h1>Project Information</h1>
<form method="post">
<table class="table table-hover">
<thead class = "table-light">
<tr>
<th>@Model.PersonnelNo</th>
</tr>
</thead>
</table>
<div class="form-control">
@Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
<input type="submit" value="Save" class="btn btn-primary"/>
</div>
</form>
这是第一次起作用,当页面加载时(.cs文件中的personnelno具有值为值),
但是当我按提交按钮时,尝试重新加载同一页面,personnelno的值无效。
有没有办法将参数从视图回到.cs文件,以便可以重复使用?
我以为@html.hiddenfor(model =&gt; model.personnelno,model.personnelno)
应该这样做,但不起作用
I have something like this (reduced for brevity)
Expenditures.cshtml.cs:
public class Expenditures: PageModel {
[BindProperty(SupportsGet = true)] public string PersonnelNo {get;set;}
public async Task OnGetAsync() { //some code
}
public async Task < IActionResult > OnPostAsync() {
return Page();
}
}
and Expenditures.cshtml
@page
@using DataAccessLibrary.Models
@model WorkInProgress.Pages.Expenditures
@{
ViewData["Title"] = "Expenditures";
}
<h1>Project Information</h1>
<form method="post">
<table class="table table-hover">
<thead class = "table-light">
<tr>
<th>@Model.PersonnelNo</th>
</tr>
</thead>
</table>
<div class="form-control">
@Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
<input type="submit" value="Save" class="btn btn-primary"/>
</div>
</form>
this works the first time, when the page loads (PersonnelNo in the .cs file has a value)
however when I press the submit button and I try to reload the same page, the value for PersonnelNo is nulled.
Is there a way to pass the parameter from the view back to the .cs file so it can be reused?
I thought the @Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
should do this, but is not working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在演示下,我可以获得价值,您可以参考它。
第一种方法
将参数添加到处理程序方法。
查看:
结果:
添加名称属性do do Model banding
添加此行
&lt; input name =“ PersonnelNo” type =“ sideen”/&gt;
之后@html.hiddenfor(model =&gt; model.personnelno,model.personnelno)
结果:
Below demo I can get the value, you can refer to it.
First way
Adding parameter to the handler method.
View:
Result:
Second way
Add name attribute to do model binding
Add this line
<input name="PersonnelNo" type="hidden" />
after@Html.HiddenFor(model => model.PersonnelNo, Model.PersonnelNo)
Result: