Group items in application table Asp.net web MVC

Asked

Viewed 323 times

3

How can I group items into 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 I 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 follows 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;

1 answer

1

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

Taking into account that the CodPro be a whole, 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 interaction foreach will go through the data as they were grouped by action, being the @item.key your CodPro.

The second foreach will interact with group items.

  • Pablo sorry ignorance, but what would be "Agruparitensemtabelaaplicacaoaspnetwebmvc"

  • the namespace of the example I created. In this case you change to the namespace of where your Product Search class is

  • +1 Pablo, thanks for the help, I did according to what reported, I managed to solve the problem.

  • I’m glad to help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.