ASP.NET-基于表列在表中放置数据

本文关键字:置数据 NET- 于表列 ASP | 更新日期: 2023-09-27 18:29:04

在ASP.NET中,我正在动态创建一个表,包括列和行。

<table>
<thead>
    <tr>
        <th scope="col">To location</th>
        @foreach (String itemName in ViewBag.itemNames)
        {
            <th scope="col">@itemName</th>
        }
    </tr>
</thead>
<tbody>
    @foreach (var transaction in Model)
    {
        <tr>
            <td>@transaction.toLocation</td>
            @foreach (var item in transaction.itemList)
            {
                <td>@item.quantity</td>
            }
        </tr>
    }
</tbody>
</table>

在thead元素中,我得到一个列,其中包含项目发送到的位置,以及事务中可能存在的每个项目的列。tbody元素中使用的itemList的名称与thead中的itemName列的名称相同,加上项目的数量。我正在为第二个foreach循环寻找这样的东西:

foreach(item in transaction.itemList)
{
    @thead.findColumn(@item.itemName) // find the column name that's the same as the itemName
    <td>@item.quantity</td> // place the data in that column
}

有人知道怎么做吗?

ASP.NET-基于表列在表中放置数据

您可以循环遍历每行的所有列名,并使用LINQ来获取您要查找的项目:

<tbody>
    @foreach (var transaction in Model)
    {
        <tr>
            <td>@transaction.toLocation</td>
            foreach(String itemName in ViewBag.itemNames)
            {
                <td>transaction.itemList.Where(x => x.itemName == itemName).Select(x => x.quantity).FirstOrDefault()</td>
            }
        </tr>
    }
</tbody>

你可能需要在文件的顶部包括这个:

@using System.Data.Linq