为什么我的对象在填充到Onget时在帖子上被无效
我有一个具有以下内容的课程(简短减少)
[BindProperty(SupportsGet = true)]
public List<Expenditure> ListExpendituresOver90Days { get; set; }
public async Task OnGetAsync(int projectId, string ExpenditureFilter, string personnelNo)
{
ListExpendituresOver90Days = ExpendituresList.ToList();
}
public async Task<IActionResult> OnPostButtonAsync()
{
return page();
}
和cshtml,带有:
@model WorkInProgress.Pages.Expenditures
<thead>
<tr>
<th class = text-start>Task Number</th>
<th class = text-start>Expenditure Type</th>
<th class = text-end>Item Date</th>
<th class = text-end>Billable</th>
<th class = text-end>Vendor Id</th>
<th class = text-start>Vendor Name</th>
<th class = text-end>Revenue Amount</th>
<th class = text-end>Comments</th>
</tr>
</thead>
<tbody>
@for (var x = 0; x < @Model.ListExpendituresOver90Days.Count(); x++)
{
@Html.HiddenFor(model => model.ListExpendituresOver90Days[x].ExpenditureItemId)
<tr>
<td class=text-start>@Model.ListExpendituresOver90Days[x].TaskNumber</td>
<td class=text-start>@Model.ListExpendituresOver90Days[x].ExpenditureType</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].BillableFlag</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].VendorId</td>
<td class=text-start>@Model.ListExpendituresOver90Days[x].SupplierName</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].ProjFuncRevenueAmount</td>
<td class=text-end>
<textarea class="form-control" form="MainForm" asp-for="@Model.ListExpendituresOver90Days[x].ExpenditureComment" rows="1"></textarea>
</td>
</tr>
}
</tbody>
<div class="form-control">
<button type="submit" class="btn btn-primary" asp-page-handler="Button">Save</button>
</div>
当页面加载时,所有列的数据都会填充所有列,而对象“ list expendituresover90days”包含具有所有属性的枚举器,所有属性都应该:
但是,当我单击“保存”按钮并调用OnPost方法时,该对象回到空,除了在评论文本中插入的值:
只有复制我的错误所需的需要,但如果有用,我将其放置在此屏幕上的所有代码:
https://gist.github.com/gasguirre/18A19D07CF1C1C1C14DB2AAFB539253461F9
我使用的部分是我用于循环的部分
I have a class with the following (reduced for brevity)
[BindProperty(SupportsGet = true)]
public List<Expenditure> ListExpendituresOver90Days { get; set; }
public async Task OnGetAsync(int projectId, string ExpenditureFilter, string personnelNo)
{
ListExpendituresOver90Days = ExpendituresList.ToList();
}
public async Task<IActionResult> OnPostButtonAsync()
{
return page();
}
and the cshtml with:
@model WorkInProgress.Pages.Expenditures
<thead>
<tr>
<th class = text-start>Task Number</th>
<th class = text-start>Expenditure Type</th>
<th class = text-end>Item Date</th>
<th class = text-end>Billable</th>
<th class = text-end>Vendor Id</th>
<th class = text-start>Vendor Name</th>
<th class = text-end>Revenue Amount</th>
<th class = text-end>Comments</th>
</tr>
</thead>
<tbody>
@for (var x = 0; x < @Model.ListExpendituresOver90Days.Count(); x++)
{
@Html.HiddenFor(model => model.ListExpendituresOver90Days[x].ExpenditureItemId)
<tr>
<td class=text-start>@Model.ListExpendituresOver90Days[x].TaskNumber</td>
<td class=text-start>@Model.ListExpendituresOver90Days[x].ExpenditureType</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].BillableFlag</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].VendorId</td>
<td class=text-start>@Model.ListExpendituresOver90Days[x].SupplierName</td>
<td class=text-end>@Model.ListExpendituresOver90Days[x].ProjFuncRevenueAmount</td>
<td class=text-end>
<textarea class="form-control" form="MainForm" asp-for="@Model.ListExpendituresOver90Days[x].ExpenditureComment" rows="1"></textarea>
</td>
</tr>
}
</tbody>
<div class="form-control">
<button type="submit" class="btn btn-primary" asp-page-handler="Button">Save</button>
</div>
when the page loads, the data gets filled for all columns, and the object "ListExpendituresOver90Days" contains the enumerator with all properties populated as it should:
however when I click on the "Save" button and this calls the onPost method, the object comes back empty, except for the value inserted in the comment TextArea:
I have tried putting only the needed to replicate my error, but in case is useful, I'm putting here a GIST with all the code that I have for this screen:
https://gist.github.com/gasguirre/18a19d07cf1c14db2aafb539253461f9
the section I'm using for the For loop that is not working properly is in "_Over90DaysExpenditureLayout.cshtml"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是网络工作的方式 - 它是无家可归的。提出GET请求时,您的PageModel将进行实例化,并调用
onget
方法。一旦发送了HTML响应,PageModel实例及其所有状态都将被销毁。当您提出发布请求时,将创建PageModel的新实例,并调用OnPostButtonAsync
方法。ongetAsync
方法不是 - 通过惯例,它仅用于获取请求。如果您希望每个支出中的所有值
在邮政请求处理程序中重组,则可以为每个字段创建隐藏字段。That is how the web works - it is stateless by design. When you make a GET request, your PageModel is instantiated and the
OnGet
method is invoked. Once the HTML response has been sent, the PageModel instance is destroyed along with all its state. When you make a POST request, a new instance of the PageModel is created and theOnPostButtonAsync
method is invoked. TheOnGetAsync
method is not - by convention it is only invoked for GET requests. If you want all values in eachExpenditure
to be reconstituted in the POST request handler, you can create hidden fields for each one.