为什么我的对象在填充到Onget时在帖子上被无效

发布于 2025-02-10 21:14:04 字数 2920 浏览 1 评论 0原文

我有一个具有以下内容的课程(简短减少)

 [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:

enter image description here

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2025-02-17 21:14:04

这就是网络工作的方式 - 它是无家可归的。提出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 the OnPostButtonAsync method is invoked. The OnGetAsync method is not - by convention it is only invoked for GET requests. If you want all values in each Expenditure to be reconstituted in the POST request handler, you can create hidden fields for each one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文