c# – Group items into table ASP.NET web MVC application

Question:

How can I group items in a table? I'm bringing these items from the database, I would like to group them by code, for example, if there is repeated code 1, I would like to group the same in 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 visualization 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;

Answer:

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

Assuming 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 iteration will iterate through the data as it was grouped by the action , @item.key being your CodPro .

The second foreach will interact with the items in the group.

Scroll to Top