2017-06-03 69 views
0

Im triyng對我的視圖進行一些計算,但我無法顯示出我的@ {}塊的結果在視圖上顯示結果

我有這張表。

<!--Table to display registered products--> 
    <table class="table table-condensed table-hover"> 
     <thead> 
      <tr> 
       <th>CODIGO</th> 
       <th>NOME</th> 
       <th>PRECO</th> 
       <th>QUANTIDADE</th> 
       <th></th> 
      </tr> 
     </thead> 
     <tbody> 
      @if (Model != null) 
      { 
       double sum = 0; 
       for (int i = 0; i < Model.Item2.Count; i++) 
       { 
        <tr> 
         @using (Html.BeginForm("AddProduct", "Sale", FormMethod.Post)) 
         { 
          <td> 
           @Html.DisplayFor(modelItem => Model.Item2[i].idProduct) 
          </td> 

          <td>@Html.DisplayFor(model => Model.Item2[i].nameProduct)</td> 
          <td> 
           <p>[email protected](model => Model.Item2[i].pricesaleProduct)</p> 
          </td> 
          <td>@Html.DisplayFor(model => Model.Item2[i].quantityProduct)</td> 
          <td> 
           <input type="submit" class="btn btn-danger" value="Deletar"> 
          </td> 
          sum += Model.Item2[i].pricesaleProduct * Model.Item2[i].quantityProduct;       
         } 
        </tr> 
       } 
      } 
     </tbody> 
    </table> 

<!--I want display "sum" here--> 
Total Value: @sum 

我想顯示「總和」出我的表,但嘗試幾個變化和獲得錯誤!

我該怎麼做?

+0

你會得到什麼錯誤? –

+0

名稱「sum」在當前上下文中不存在 –

+0

在視圖頂部聲明「sum」(在'

'標籤之前) –

回答

0

您已在if(){}區塊內聲明總和,因此您無法訪問它。像這樣移動它外面,它會工作

double sum = 0; 

@if (Model != null) 
{ 

     ............ 
     ............ 
}