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.