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 perform the sum of the months, creating a monthly subtotal. With help I managed to work, leaving the result as the example:
Jsfiddle example
But I need the sum to restart at the turn of the year. This logic that I am not able to elaborate.
I need the end result to turn something like:
Example Expected result
My controller is like this:
public ActionResult Index()
{
//realizo a busca no BD
var usuario =
previdenciaRepository.Previdencias.Where(p => p.CdMatricula == matricula && p.SqContrato == contrato).ToList();
return View(usuario);
}
And in the view I do the calculation to add the returned data, so:
<table border="1">
<thead>
<tr>
<th rowspan="2">
Mês
</th>
<th colspan="4">
<p align="center">
Contribuinte
</p>
</th>
</tr>
<tr>
<th>
%
</th>
<th>
R$ Ano
</th>
<th>
Acumulado R$
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Previdencia.GroupBy(g => g.NmPessoa))
{
double subtotal = 0;
foreach (var contribuicoes in item.ToList())
{
subtotal += contribuicoes.Contribuinte;
<tr>
<td>
@Html.DisplayFor(modelItem => contribuicoes.dtCompetencia)
</td>
<td>
11
</td>
<td>
@Html.DisplayFor(modelItem => contribuicoes.Contribuinte)
</td>
<td>
@subtotal.ToString("c")
</td>
</tr>
}
}
</tbody>
</table>
My Model is like this:
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; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dtCompetencia { get; set; }
}
As always, you helped me again. Just a doubt. To contain the two fields, how should I proceed? The total and counting per year? Thus: http://jsfiddle.net/randrade/9qj04130/1/
– Randrade
@Renilsonandrade It can be by month too. Or it can be by a grouping assembled by you. Creativity is the limit.
– Leonel Sanches da Silva
I understood this part. However I need to save the total sum (without zeroing per year) in another field (as was done with the variable subtotal). Would you be able to do this by creating another variable? Or would you rather I open another question?
– Randrade
@Renilsonandrade No need. I already put in the answer. See if it meets you.
– Leonel Sanches da Silva
Thank you so much again, it was just that. Sometimes I even get puzzled at the ease of my problems. But that’s the way it is. rsrs
– Randrade