Error adding values of a Foreach in MVC3 view C #

0

Currently I have an error adding some values of a foreach, the view is of extension .cshtml razor in C #, the code is as follows:

<div class="panel panel-primary">
              <!-- Default panel contents -->
              <div class="panel-heading">CC</div>
              <div class="panel-body">
                              <table class="table" style="width:100%">
                                <thead style="background-color:#d6e7ee">
                                    <tr>
                                        <th>cc</th>
                                        <th>Moneda</th>
                                        <th>Porcentaje</th>
                                        <th>Costo</th>
                                    </tr>
                                </thead>
                                <tbody style="background-color:#f1f8f8" >
                                    @{
                                        Double TotalPorcentaje=0;
                                        Double TotalCosto=0;
                                        foreach (var item in Model.ListarCostos)
                                        {
                                            <text>
                                                <tr>
                                                    <td>@item.strNombreCentrocosto</td>
                                                    <td>@item.strSimboloMoneda</td>
                                                    <td>@item.nPercentageCentroCosto %</td>
                                                    <td>@item.nCosto</td>
                                                </tr>
                                                [email protected];
                                                [email protected];
                                            </text>                                
                                        }
                                    }
                                    @{
                                        <text>
                                                <tr>
                                                    <td colspan="2"><strong>TOTAL:</strong></td>
                                                    <td><strong>TotalPorcentaje</strong></td>
                                                    <td><strong>TotalCosto</strong></td>
                                                </tr>
                                        </text>
                                    }
                                </tbody>
                            </table>
              </div>
            </div>

Can someone give me some references of the correct syntax, thank you very much.

    
asked by DAES 17.09.2017 в 23:56
source

2 answers

1

If you mention that an error occurs you will be grateful if you give us the message of it, not all exceptions are evident just by reading the code.

Regarding the code that you attach, I suggest obtaining the totals by means of the extension method Sum () and painting them in the 'tfoot' section, for example:

<table>
<thead></thead>
<tbody></tbody>
<tfoot>
    <tr>
        <td><strong>@Model.ListarCostos.Sum(x => x.nPercentageCentroCosto)</strong></td>
        <td><strong>@Model.ListarCostos.Sum(x => x.MaBRID)</strong></td>
    </tr>
</tfoot>
</table>

You can apply the presentation format to the results using the .ToString method ("P2 | N2").

    
answered by 18.09.2017 / 01:02
source
0

Line expressions are used to print data to the client, not to do operations with them. You should also move the sum operation out of the text tag so you can do the operation without using block code.

Replaces:

//...
</tr>                                                
  [email protected];                                              
  [email protected];
</text>  
//..

By:

   //...
    </tr>    
   </text>                                            
    TotalPorcentaje=TotalPorcentaje + item.nPercentageCentroCosto;                                              
    TotalCosto=TotalCosto + item.nCosto;
   //..
    
answered by 18.09.2017 в 02:48