Make sum of all searched data

Asked

Viewed 32 times

1

I am making a report, I was able to make him bring me the data of specific dates, but I need now that he brings me the sum of everything that was selected, I made an attempt but does not bring the sum of everything, only some things were added up.

My View:

Report Sale by Order/Period From: @Viewbag.dataRead to @Viewbag.dataFinal

                <tr>

                    <th>N° Carro </th>
                    <th>Quant. </th>
                    <th>Km </th>
                    <th>Valor Unitário </th>
                    <th>Sub Total </th>
                    <th>Data Abastecimento </th>
                </tr>
                        @{foreach (var item in Model)
                            {
                                foreach(var iten in Model)
                                {
                             <tr>
                             <td> @Html.DisplayFor(modelItem => iten.NumCarro.NCarro) </td>
                             <td> @Html.DisplayFor(modelItem => iten.Litro) </td>
                             <td> @Html.DisplayFor(modelItem => iten.Km) </td>
                             <td> @Html.DisplayFor(modelItem => iten.VlrUnit) </td>
                             <td> @Html.DisplayFor(modelItem => iten.TotalGasto) </td>
                             <td> @Html.DisplayFor(modelItem => iten.DtAbastecido) </td>
                             </tr>
                            }
                                }
                        }

He is doing the sum directly in the view, I do not know if it has to apply by the controller, in my view what returns me is this sum.

So returns the sum: inserir a descrição da imagem aqui

  • I think the problem is in the above loop...

  • I put more things before the view

  • puts.... has 2 nested foreach there...

  • 2

    just take out the first foreach...the sum is correct

  • could put the answer only I to give right there

1 answer

1


The problem is in your loop that repeats the data. The sum is correct. Just remove the first foreach

@{

//Remover esse -> foreach (var item in Model) 

foreach(var iten in Model)
{
    <tr>
    <td> @Html.DisplayFor(modelItem => iten.NumCarro.NCarro) </td>
    <td> @Html.DisplayFor(modelItem => iten.Litro) </td>
    <td> @Html.DisplayFor(modelItem => iten.Km) </td>
    <td> @Html.DisplayFor(modelItem => iten.VlrUnit) </td>
    <td> @Html.DisplayFor(modelItem => iten.TotalGasto) </td>
    <td> @Html.DisplayFor(modelItem => iten.DtAbastecido) </td>
    </tr>
}

}

Browser other questions tagged

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