[英]Filtering Data Table based on column values containing special charactors in asp.net c#
[英]ASP.NET - place data in table based on table column
在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
}
有人知道怎么做嗎?
您可以遍歷每一行的所有列名,然后使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.