Add values from object list columns in Razor

Asked

Viewed 1,309 times

2

I have a table, where I receive data from a View (SQL Server) and perform some operations on my page. On this page, I have a filter to show only the pages referring to the selected year.

Even this part is all right, along with the filter, as can be seen in this example.

The operations are performed, entirely in my View, which follows below:

<table id="item-list" class="item-list table" style="margin-bottom: 0px;" border="1">
    <thead>
        <tr>
            <th rowspan="2">
                Mês
            </th>
            <th rowspan="2">
                Remuneração<br />
                Total R$
            </th>
            <th colspan="4">
                <p align="center">
                    Contribuinte
                </p>
            </th>
            <th colspan="4">
                <p align="center">
                    Município
                </p>
            </th>
        </tr>
        <tr  bgcolor="#ffffff">
            <th>
                %
            </th>
            <th>
                Mês R$
            </th>
            <th>
                Ano R$
            </th>
            <th>
                Acumulado R$
            </th>
            <th>
                %
            </th>
            <th>
                Mês R$
            </th>
            <th>
                Ano R$
            </th>
            <th>
                Acumulado R$
            </th>
        </tr>
    </thead>
    <tbody>
        @*Variáveis para somar os totais dos campos*@
        @{
            double totalContribuinte = 0;
            double totalMunicipio = 0;
        }

        @foreach (var item in Model.Previdencia.GroupBy(g => new {g.NmPessoa, g.dtCompetencia.Value.Year}))
        {
        @*Variáveis para somar os totais dos campos por ano*@
            double subtotalContribuinte = 0;
            double subtotalMunicipio = 0;

            foreach (var contribuicoes in item.ToList())
            {

            @*Realizam as somas dos campos*@
                subtotalContribuinte += contribuicoes.Contribuinte;
                totalContribuinte += contribuicoes.Contribuinte;

                subtotalMunicipio += contribuicoes.BaseCalculo;
                totalMunicipio += contribuicoes.BaseCalculo;

            @*Adiciono a classe igual a selecionada no select*@

                <tr class="@contribuicoes.dtCompetencia.Value.Year" bgcolor="#ffffff">
                    <td align="right">
                        @contribuicoes.dtCompetencia.Value.Month.ToString("00")
                    </td>
                    <td align="right">
                        @contribuicoes.BaseCalculo.ToString("N")
                    </td>
                    <td align="right">
                        11
                    </td>
                    <td align="right">
                        @contribuicoes.Contribuinte.ToString("N")
                    </td>
                    <td align="right">
                        @subtotalContribuinte.ToString("N")
                    </td>
                    <td align="right">
                        @totalContribuinte.ToString("N")
                    </td>

                    <td align="right">
                        11
                    </td>
                    <td align="right">
                        @contribuicoes.BaseCalculo.ToString("N")
                    </td>
                    <td align="right">
                        @subtotalMunicipio.ToString("N")
                    </td>
                    <td align="right">
                        @totalMunicipio.ToString("N")
                    </td>
                </tr>

            }
        }
    </tbody>
</table>

My ViewModel used, with the fields:

public class ExtratoPrevidenciaViewModel
    {
        public int Codigo { get; set; }
        public int CdMatricula { get; set; }
        public Int16 SqContrato { get; set; }

        public string NmPessoa { get; set; }

        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? dtCompetencia { get; set; }

        public double Contribuinte { get; set; }
        public double Municipio { get; set; }
        public double BaseCalculo { get; set; }

    }

My problem: How to accomplish the sum of all values, per column, separating by year, as is mine GroupBy ?

The expected end result, would look something like this.

1 answer

2


If I were you, I’d use one more <tr> to sum up, more or less like this:

<tbody>
    @*Variáveis para somar os totais dos campos*@
    @{
        double totalContribuinte = 0;
        double totalMunicipio = 0;
    }

    @foreach (var item in Model.Previdencia.GroupBy(g => new {g.NmPessoa, g.dtCompetencia.Value.Year}))
    {
    @*Variáveis para somar os totais dos campos por ano*@
        double subtotalContribuinte = 0;
        double subtotalMunicipio = 0;

        foreach (var contribuicoes in item.ToList())
        {

        @*Realizam as somas dos campos*@
            subtotalContribuinte += contribuicoes.Contribuinte;
            totalContribuinte += contribuicoes.Contribuinte;

            subtotalMunicipio += contribuicoes.BaseCalculo;
            totalMunicipio += contribuicoes.BaseCalculo;

        @*Adiciono a classe igual a selecionada no select*@

            <tr class="@contribuicoes.dtCompetencia.Value.Year" bgcolor="#ffffff">
                <td align="right">
                    @contribuicoes.dtCompetencia.Value.Month.ToString("00")
                </td>
                <td align="right">
                    @contribuicoes.BaseCalculo.ToString("N")
                </td>
                <td align="right">
                    11
                </td>
                <td align="right">
                    @contribuicoes.Contribuinte.ToString("N")
                </td>
                <td align="right">
                    @subtotalContribuinte.ToString("N")
                </td>
                <td align="right">
                    @totalContribuinte.ToString("N")
                </td>

                <td align="right">
                    11
                </td>
                <td align="right">
                    @contribuicoes.BaseCalculo.ToString("N")
                </td>
                <td align="right">
                    @subtotalMunicipio.ToString("N")
                </td>
                <td align="right">
                    @totalMunicipio.ToString("N")
                </td>
            </tr>

        }

        <tr>
            <td>Totais</td>
            <td>@item.Sum(c => c.BaseCalculo)</td>
            <td></td>
            <td>@item.Sum(c => c.Contribuinte)</td>
            <td></td>
            <td>@subtotalContribuinte.ToString("N")</td>
            <td>@totalContribuinte.ToString("N")</td>
            <td></td>
            <td>@subtotalMunicipio.ToString("N")</td>
            <td>@totalMunicipio.ToString("N")</td>
        </tr>
    }
</tbody>
  • 1

    After seeing your answer, I found that I was on the right track, and the problem was in my js filter. But that was it. Thanks for the help again.

Browser other questions tagged

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