如何在asp.net mvc3中显示矩阵表? [英] How can I display a matrix table in asp.net mvc3?
本文介绍了如何在asp.net mvc3中显示矩阵表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个小项目中涉及使用实体框架和asp.net mvc3在矩阵视图中显示多对多关系数据库。涉及的三个表格是SalesPerson(行标签),产品(列标签)和销售额:
如何在asp.net mvc3中开发/生成这种视图?
< table>
< tr>
< th>< / th>
@foreach(模型中的变量)
{
foreach(var p in m.Products)
{
< th> @ p.ProductName< / th> ;
}
}
< / tr>
@foreach(模型中的var m)
{
foreach(var s in m.SalesPersons)
{
< tr>
< td> @ s.PersonName< / td>
< / tr>
}
}
@ *销售:a.Amount * @
< / table>
解决方案
使用与此类似的LINQ查询转换数据
var salesTable =
from s in m.S.Sales
group s by s.SalesPerson.Label into g
选择新的
{
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>
}
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:
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>
解决方案
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>
}
这篇关于如何在asp.net mvc3中显示矩阵表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文