如何在 html 数据表视图中显示 IEnumerable 数据分组 ID?

How to display IEnumerable data grouped id in html datatable view?

我有一个数据 table 并且需要对值进行分组,因为只有一列 (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因为我不知道这些东西是什么。)