C# ASP.NET-基于表列在表中放置数据
在ASP.NET中,我动态创建一个表,包括列和行C# ASP.NET-基于表列在表中放置数据,c#,asp.net,C#,Asp.net,在ASP.NET中,我动态创建一个表,包括列和行 <table> <thead> <tr> <th scope="col">To location</th> @foreach (String itemName in ViewBag.itemNames) { <th scope="col">@itemName</th>
<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>
定位
@foreach(ViewBag.itemName中的字符串itemName)
{
@项目名称
}
@foreach(模型中的var事务)
{
@交易地点
@foreach(transaction.itemList中的var项)
{
@项目.数量
}
}
在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
}
foreach(transaction.itemList中的项目)
{
@thead.findColumn(@item.itemName)//查找与itemName相同的列名
@item.quantity//将数据放在该列中
}
有人知道怎么做吗?您可以循环查看每行的所有列名,并使用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