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:
I need each month to make the sum with the previous month, thus staying:
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.
That’s exactly what I need. Thank you!
– Randrade
Now, doing these calculations in the View I will have performance problems?
– Randrade
@Renilsonandrade I don’t think so. The biggest performance problem, if it occurs, would be in the Controller.
– Leonel Sanches da Silva
Vlw @Ciganomorrisonmendez, thanks again for your help.
– Randrade