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

标签 c# html asp.net-mvc razor matrix

我正在进行一个涉及使用 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/

相关文章:

html - 纠正浏览器缩放

javascript - jQuery - 将更改应用于与变量号匹配的元素

javascript - 如何使用 jquery 禁用列表框中的多项选择?还是javascript?

c# - UPS API - LabelLinksIndicator - 未设置对象实例的对象引用

c# - 为什么 InterfaceInterceptor 不比 TransparentProxyInterceptor 快?

c# - 保护 web.config 设置

javascript - 尝试从模型获取特定属性时出现对象对象错误

c# - 无法在 Raspberry Pi 上构建 Azure IOT SDK

javascript - 在页脚上粘贴 div 元素

c# - 存储用于 SQL 和 C# 的常量