null对象从foreach内部的textarea传递到onsost的值时(对象是可以的)

发布于 2025-02-09 13:48:00 字数 2348 浏览 1 评论 0原文

我有以下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 技术交流群。

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

发布评论

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

评论(1

日暮斜阳 2025-02-16 13:48:00

我同意@pcalkins,并且由于您需要在表中发布所有行,因此您需要确保&lt; textarea&gt;具有独特的名称以确保所有行都能可以提交,因此您必须使用@for而不是@foreach

我在我的身边进行了测试,请注意我定义了list&lt; eppenditures&gt;,而不是ienumerable

public class Expenditures
{
    public int ExpenditureItemId { get; set; }
    public string? ExpenditureComment { get; set; }
}

public class ViewMode {
    public List<Expenditures> ExpendituresList { get; set; }
}

,这是我的页面:

@model ViewMode
<form method="post" asp-action="saveData">
<table>
    <thead>
    <tr>
        <th class = text-start>Task Number</th>
        <th class = text-end>Comments</th>
    </tr>
    </thead>
    <tbody>
    @for (var x = 0; x < Model.ExpendituresList.Count(); x++)
    {
        <tr>
            <td class = text-start asp-for="@Model.ExpendituresList[x].ExpenditureItemId">@Model.ExpendituresList[x].ExpenditureItemId</td>
            <td class=text-end>
                <textarea class="form-control" asp-for="@Model.ExpendituresList[x].ExpenditureComment"  rows="1"></textarea>
            </td>
        </tr>
    }
    </tbody>
</table>
<div class="form-control">
    <button type="submit" class="btn btn-primary">Save</button>
</div>
</form>

这是我的控制器:

    [HttpPost]
    public void saveData( ViewMode vms)
    {
        var a = vms;
    }

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 of IEnumerable

public class Expenditures
{
    public int ExpenditureItemId { get; set; }
    public string? ExpenditureComment { get; set; }
}

public class ViewMode {
    public List<Expenditures> ExpendituresList { get; set; }
}

And this is my page:

@model ViewMode
<form method="post" asp-action="saveData">
<table>
    <thead>
    <tr>
        <th class = text-start>Task Number</th>
        <th class = text-end>Comments</th>
    </tr>
    </thead>
    <tbody>
    @for (var x = 0; x < Model.ExpendituresList.Count(); x++)
    {
        <tr>
            <td class = text-start asp-for="@Model.ExpendituresList[x].ExpenditureItemId">@Model.ExpendituresList[x].ExpenditureItemId</td>
            <td class=text-end>
                <textarea class="form-control" asp-for="@Model.ExpendituresList[x].ExpenditureComment"  rows="1"></textarea>
            </td>
        </tr>
    }
    </tbody>
</table>
<div class="form-control">
    <button type="submit" class="btn btn-primary">Save</button>
</div>
</form>

This is my controller:

    [HttpPost]
    public void saveData( ViewMode vms)
    {
        var a = vms;
    }

enter image description here

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