null对象从foreach内部的textarea传递到onsost的值时(对象是可以的)
我有以下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></th>
<th class = text-start>Vendor Name</th>
<th class = text-end>Revenue Amount</th>
<th class = text-end>Comments</th>
</tr>
</thead>
<tbody>
@foreach (var expenditure in Model.ExpendituresList)
{
<tr>
<td class = text-start>@expenditure.TaskNumber</td>
<td class = text-start>@expenditure.ExpenditureType</td>
<td class = text-end>@expenditure.ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
<td class = text-end>@expenditure.BillableFlag</td>
<td class = text-end>@expenditure.VendorId</td>
<td></td>
<td class = text-start>@expenditure.SupplierName</td>
<td class = text-end>@expenditure.ProjFuncRevenueAmount</td>
@Html.HiddenFor(model => expenditure.ExpenditureItemId)
<td class=text-end>
<textarea class="form-control" asp-for="@Model.ExpendituresList.FirstOrDefault(x => x.ExpenditureItemId == expenditure.ExpenditureItemId)!.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>
我试图拥有类似的东西:
然后从那里捕获文本中的文本,并将其发送给带有文本的值(我想要的值) <代码> expenditurecomment 填充
支出的类是
[BindProperty(SupportsGet = true)]
public IEnumerable<Expenditure> ExpendituresList { get; set; }
公共十进制eppenturetemidemid {get; set; ;}
在单击“保存”对象elevenitulist时,
我认为我的问题是我设置&lt; textarea&gt;
元素。但是我一直在尝试不同的设置,无法使其正常工作
I have the following CSHTML class:
@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></th>
<th class = text-start>Vendor Name</th>
<th class = text-end>Revenue Amount</th>
<th class = text-end>Comments</th>
</tr>
</thead>
<tbody>
@foreach (var expenditure in Model.ExpendituresList)
{
<tr>
<td class = text-start>@expenditure.TaskNumber</td>
<td class = text-start>@expenditure.ExpenditureType</td>
<td class = text-end>@expenditure.ExpenditureItemDate.ToString("dd-MMM-yyyy")</td>
<td class = text-end>@expenditure.BillableFlag</td>
<td class = text-end>@expenditure.VendorId</td>
<td></td>
<td class = text-start>@expenditure.SupplierName</td>
<td class = text-end>@expenditure.ProjFuncRevenueAmount</td>
@Html.HiddenFor(model => expenditure.ExpenditureItemId)
<td class=text-end>
<textarea class="form-control" asp-for="@Model.ExpendituresList.FirstOrDefault(x => x.ExpenditureItemId == expenditure.ExpenditureItemId)!.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>
in which I'm trying to have something like this:
and then from there, capture the text in the textArea and send it to the Post with the value for the textArea (that I want to put in expenditure.ExpenditureComment
the class that fills the expenditures is
[BindProperty(SupportsGet = true)]
public IEnumerable<Expenditure> ExpendituresList { get; set; }
with the properties for public decimal ExpenditureItemId { get; set; }
and public string? ExpenditureComment { get; set; }
however when debbugging the OnPost after clicking "Save" the object ExpenditureList returns empty.
I think my issue is in the way I'm setting the <TextArea>
element. but I've been trying different setups and not being able to make it work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意@pcalkins,并且由于您需要在表中发布所有行,因此您需要确保
&lt; textarea&gt;
具有独特的名称以确保所有行都能可以提交,因此您必须使用@for
而不是@foreach
。我在我的身边进行了测试,请注意我定义了
list&lt; eppenditures&gt;
,而不是ienumerable
,这是我的页面:
这是我的控制器:
I agree with @pcalkins, and since you need to post all rows in the table, so you need to make sure each line of the
<textarea>
have the unique name to make sure all the rows can be submit, so you have to use@for
instead of@foreach
.I did a test in my side and pls note I defined
List<Expenditures>
instead ofIEnumerable
And this is my page:
This is my controller: