Perform field sum in the table

Asked

Viewed 973 times

1

I have a View that returns data that the taxpayer paid the pension in the current year, where I return the monthly payment of the same. I need to sum up the months by creating a monthly subtotal.

Currently I have this data in my View:
inserir a descrição da imagem aqui

I need each month to make the sum with the previous month, thus staying: inserir a descrição da imagem aqui

This data I return for an action in my controller:

 public ActionResult Index()
        {
            var usuario =
                previdenciaRepository.Previdencias.Where(p => p.CdMatricula == matricula && p.SqContrato == contrato).ToList();       

            return View(usuario);
        }

And return the data in my View:

@model IEnumerable<PortalRH.DomainModel.Entities.Previdencia>

@{
    ViewBag.Title = "Index";
}    
<h2>Index</h2>

<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.NmPessoa)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Contribuinte)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.NmPessoa)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Contribuinte)
            </td>  
        </tr>
    }

</table>

Welfare class:

 public class Previdencia
    {
        [Key]
        public Int64 Cod_Previdencia { get; set; }
        public int CdMatricula { get; set; }
        public Int16 SqContrato { get; set; }
        public string NmPessoa { get; set; }
        public double Contribuinte { get; set; }
    }

I tried to perform the calculation by means of a For()in View, staying like this:

@model IEnumerable<PortalRH.DomainModel.Entities.Previdencia>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table class="table">
    <tr>

        <th>
            @Html.DisplayNameFor(model => model.NmPessoa)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Contribuinte)
        </th>
        <th>
            Ano contribuinte
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.NmPessoa)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Contribuinte)
            </td>  
            <td>
                @{
                    var subTotal = 0.0;
                    int contador = item.Contribuinte.ToString("c").Count();
                    for (int i = 0; i < contador; i++)
                    {
                        subTotal += item.Contribuinte;
                    }


                }
              @subTotal
            </td>
        </tr>
    }

</table>

He walks the For() correctly. But in this case the value of @subTotal will always be the sum of all the data.

1 answer

0


In your place, I would group the records by NmPessoa or some identifier field of the person:

@foreach (var item in Model.GroupBy(g => g.NmPessoa))
{
    var subtotal = 0;

    foreach (var contribuicoes in item.ToList()) 
    {
        subtotal += contribuicoes.Contribuinte;

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Key)
            </td>
            <td>
                @Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
            </td> 
            <td>
                @subtotal
            </td>
        </tr>
    }
}

I don’t know if I understand correctly. If you need, comment that I change the answer.

I didn’t test that code.

  • That’s exactly what I need. Thank you!

  • Now, doing these calculations in the View I will have performance problems?

  • @Renilsonandrade I don’t think so. The biggest performance problem, if it occurs, would be in the Controller.

  • 1

    Vlw @Ciganomorrisonmendez, thanks again for your help.

Browser other questions tagged

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