簡體   English   中英

如何在asp.net mvc3中顯示矩陣表?

[英]How can I display a matrix table in asp.net mvc3?

我正在進行一個涉及使用實體框架和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>

使用類似於此的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>
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM