gpt4 book ai didi

c# - 如何在 asp.net mvc3 中显示矩阵表?

转载 作者:太空狗 更新时间:2023-10-29 16:02:22 25 4
gpt4 key购买 nike

我正在进行一个涉及使用 Entity Framework 和 asp.net mvc3 在矩阵 View 中显示多对多关系数据库的小项目。涉及的三张表分别是SalesPerson(行标签)、Product(列标签)和Sales:

Matrix Table

如何在 asp.net mvc3 中开发/生成这种 View ?

<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>

最佳答案

使用类似于此的 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>
}

关于c# - 如何在 asp.net mvc3 中显示矩阵表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10647786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com