Group items in table application asp.net web MVC

2

How can I group items in a table? I am bringing these items from the database, I would like to group by the code of the same, for example, if there is repeated code 1 would like to group it into a single item. This is the controller class. This is the Controller I'm using.

// GET: PesquisaProdutoes
    public ActionResult RelatorioPConfirmado()
    {
        var pesquisa = db.PesquisaProdutoes.GroupBy(c => c.CodPro);
        return View(pesquisa.ToList());
    }

Below is the CSHTML preview file.

 <table class="table table-bordered" id="dataTable" cellspacing="0">
                    <thead>
                        <tr>
                            <th>@Html.DisplayNameFor(model => model.CodPro)</th>
                            <th>@Html.DisplayNameFor(model => model.CodBar)</th>
                            <th>@Html.DisplayNameFor(model => model.Nome)</th>
                            <th>@Html.DisplayNameFor(model => model.Valor)</th>
                            <th>@Html.DisplayNameFor(model => model.Qtd)</th>
                            <th>@Html.DisplayNameFor(model => model.Total)</th>
                            <th>@Html.DisplayNameFor(model => model.Confimado)</th>
                            <th>@Html.DisplayNameFor(model => model.DataPesquisa)</th>
                    </thead>
                    @foreach (var item in Model)
                    {
                        if (item.Confimado.Equals(true))
                        {
                                <tr>
                                    <td>@Html.DisplayFor(modelItem => item.CodPro)</td>
                                    <td>@Html.DisplayFor(modelItem => item.CodBar)</td>
                                    <td>@Html.DisplayFor(modelItem => item.Nome)</td>
                                    <td>@Model.Sum(v => v.Valor)</td>
                                    <td>@Model.Sum(q => q.Qtd)</td>
                                    <td>@Model.Sum(t => t.Total)</td>
                                    <td>@Html.DisplayFor(modelItem => item.Confimado)</td>
                                    <td>@Html.DisplayFor(modelItem => item.DataPesquisa)</td>
                                </tr>
                            }
                    }

                </table>

This is my Data model.

 [Display(Name = "Codigo Produto")]
public int CodPro { get; set; }

[Display(Name = "Codigo de Barras")]
public string CodBar { get; set; }

[Display(Name = "Nome Produto")]
public string Nome { get; set; }

[Display(Name = "Valor unitário")]
public decimal Valor { get; set; }

[Display(Name = "Quantidade informada")]
public decimal Qtd { get; set; }

[Display(Name = "Valor Total")]
public decimal Total { get; set; }

[Display(Name = "Produto foi confirmado?")]
public bool Confimado { get; set; }

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DataPesquisa { get; set; } = DateTime.Now;
    
asked by anonymous 03.12.2017 / 15:27

1 answer

0

You must type your view to receive a model of type IEnumerable<IGrouping<out TKey, out TElement>> .

Considering that CodPro is an integer, your view would look like this.

@model IEnumerable<IGrouping<int, Models.PesquisaProduto>>
<table class="table table-bordered" id="dataTable" cellspacing="0">
    <thead>
        <tr>
            <th>Código produto</th>
        </tr>
    </thead>
    @foreach (var item in Model)
    {
        <tr>
            <td>@item.Key</td>            
        </tr>
        <tr>
            <td>
                <table>
                    <thead>
                        <tr>
                            <td>
                                Nome
                            </td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var p in item)
                        {
                            <tr>
                               <td>
                                   @p.Nome
                               </td>
                            </tr>
                        }                        
                    </tbody>
                </table>
            </td>
        </tr>
    }
</table>

The first foreach interaction will cycle through the data as it was grouped by action, with @item.key its CodPro .

The second foreach will interact in group items.

    
03.12.2017 / 16:23