在视图上显示结果

时间:2017-06-03 19:12:20

标签: c# asp.net-mvc-4

我想对我的视图进行一些计算,但是我无法从我的@ {}块中显示结果

我有这张桌子。

    <!--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>R$@Html.DisplayFor(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

我想在表格中显示“总和”,但尝试了几种变化并获得错误!

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您已在if(){}块内声明了sum,因此您无法访问它。像这样把它移到外面它会起作用

 double sum = 0;

 @if (Model != null)
 {  

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