我有一个数据表,需要对值进行分组,因为只有一列(Paxs)没有重复数据,其他数据预计会显示在一行中。
是这样的:Actual
应该是:Should be
<table class="table table-bordered table-condensed table-striped">
<caption>Serviços</caption>
<thead>
<tr>
<th>Código</th>
<th>Descrição Resumida</th>
<th>Status</th>
<th>Identificador Reserva</th>
<th>Paxs</th>
@*<th>Documentos Anexados</th>
<th></th>
<th>CTB</th>
<th>Comercial</th>
<th>Site</th>*@
</tr>
</thead>
<tbody>
@foreach (var itemServico in Model.Servicos.Take(10))
{
<tr>
<td>@itemServico.CodServico</td>
<td>@itemServico.DescTipo</td>
<td>@itemServico.StatusReserva</td>
<td>@itemServico.IdentReserva</td>
<td>@itemServico.PaxsReserva</td>
<td>
<table>
@foreach (var itemFileIntra in Model.ServicoDocs)
{
<tr>
<td>@itemFileIntra.NomeArquivo</td>
<td>
<span class="input-group-addon">
<button aria-hidden="false" class="md-icon-button
md-accent md-button md-ink-ripple">
<i class="glyphicon glyphicon-remove"></i>
</button>
<button aria-hidden="false" class="md-icon-button
md-accent md-button md-ink-ripple">
<i class="glyphicon glyphicon-search"></i>
</button>
<button aria-hidden="false" class="md-icon-button
md-accent md-button md-ink-ripple">
<i class="glyphicon glyphicon-envelope"></i>
</button>
</span>
</td>
<td>@itemFileIntra.CTB</td>
<td>@itemFileIntra.COM</td>
<td>@itemFileIntra.Site</td>
</tr>
}
</table>
</td>
</tr>
}
</tbody>
一列有不同的数据,其他数据预计会显示在一行中。
如果您创建反映要显示的数据的模型,则会更容易。这样,操作就会在代码中进行,视图就会跟随模型中的内容。
这不是最优雅的例子。以此为例。
你有这个:
public class TheModelYouHave
{
public string Servico { get; set; }
public string DescTipo { get; set; }
public string StatusReserva { get; set; }
public string IdentReserva { get; set; }
public string PaxsReserva { get; set; }
}
你想要的是这个:
public class TheModelYouWant
{
public Dictionary<string, IEnumerable<TheModelYouHave>> ModelsGroupedByPaxsReserva { get; }
= new Dictionary<string, IEnumerable<TheModelYouHave>>();
}
这是一本字典。关键是PaxsReserva
的每个不同值,值是PaxsReserva
具有相同值的所有项的集合。
要将模型映射到您想要的模型:
public static TheModelYouWant ToTheModelYouWant(IEnumerable<TheModelYouHave> source)
{
var grouped = source.GroupBy(src => src.PaxsReserva, src => src);
var result = new TheModelYouWant();
foreach(var group in grouped)
result.ModelsGroupedByPaxsReserva.Add(group.Key, group);
return result;
}
现在在你看来:
@foreach(var paxs in Model.ModelsGroupedByPaxsReserva.Keys)
然后获取属于该键的各个模型:
var items = Model.ModelsGroupedByPaxsReserva[paxs];
(我称之为items
因为我不知道这些是什么。)