Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm on a little project that involves using entity framework and asp.net mvc3 to display many to many relationship database in a matrix view. The three tables involved are SalesPerson (Row label), Product(Column label) and Sales:

Matrix Table

How can I develop/generate this kind of view in 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>
like image 773
ArchieTiger Avatar asked Oct 07 '22 19:10

ArchieTiger


People also ask

What is ASP net MVC3?

ASP.NET MVC 3 provides a Model-View-Controller (MVC) framework on top of the ASP.NET 4 runtime. Details.


1 Answers

Transform your data using a LINQ query similar to this one

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)
    };

Generating table rows is then easy

@foreach (var tableRow in salesTable)
{
    <tr>
        <td>@tableRow.rowKey</td>
        @foreach (var sale in tableRow.rowData)
        {
            <td>@sale.Amount</td>
        }
    </tr>
}
like image 177
Juraj Suchár Avatar answered Oct 10 '22 08:10

Juraj Suchár