我正在进行一个涉及使用实体框架和asp.net mvc3在矩阵视图中显示多对多关系数据库的小项目。涉及的三个表是SalesPerson(行标签),产品(列标签)和销售:
如何在asp.net mvc3中开发/生成这种视图?
<table>
<tr>
<th></th>
@foreach (var m in Model)
{
foreach (var p in m.Products)
{
<th>@p.ProductName</th>
}
}
</tr>
@foreach (var m in Model)
{
foreach (var s in m.SalesPersons)
{
<tr>
<td>@s.PersonName</td>
</tr>
}
}
@*Sales: a.Amount*@
</table>
答案 0 :(得分:4)
使用与此类似的LINQ查询转换数据
var salesTable =
from s in m.Sales
group s by s.SalesPerson.Label into g
select new
{
rowKey = g.Key,
rowData = g.Select(s => new { Product = s.Product, Amount = s.Amount }).OrderBy(s => s.Product.Label)
};
生成表格行很容易
@foreach (var tableRow in salesTable)
{
<tr>
<td>@tableRow.rowKey</td>
@foreach (var sale in tableRow.rowData)
{
<td>@sale.Amount</td>
}
</tr>
}