Perform field sum in table

1

I have a view that returns me data that the taxpayer paid the pension in the current year, where I return the monthly payment of the same. I need to complete the sum of the months, creating a monthly subTotal.

Currently I have this data in my View:

Ineedeverymonthtomakethesumofthepreviousmonth,thus:

This data I return by 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 I 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>

Pension Fund 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 the calculation with a For() in the View, thus:

@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>

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

    
asked by anonymous 05.03.2015 / 15:57

1 answer

0

In its 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 do not know if I understood correctly. If you need, comment on my response.

I have not tested this code.

    
05.03.2015 / 16:29