Reputation: 7838
In ASP.NET, I'm creating a table dynamically, both columns and rows.
<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>
In the thead element, I get a column with the place an item is sent to, and a column for every item that could be in the transaction. The itemList used in the tbody element holds the same names as ones of the itemName columns in the thead, plus a quantity of the item. What I'm looking for is something like this for the second foreach loop:
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
}
Anyone know how to do this?
Upvotes: 0
Views: 61
Reputation: 738
You could loop through all the column names for each row and use LINQ to get the item you are looing for:
<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>
you might need to include this at the top of the file:
@using System.Data.Linq
Upvotes: 1