C# 在视图中显示结果

C# 在视图中显示结果,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我想对我的观点进行一些计算,但我无法从我的@{}块中显示结果 我有这张桌子 <!--Table to display registered products--> <table class="table table-condensed table-hover"> <thead> <tr> <th>CODIGO</th>

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

我有这张桌子

    <!--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

科迪戈
诺姆
预科
量子化
@如果(型号!=null)
{  
双和=0;
对于(int i=0;iModel.Item2[i].idProduct)
@Html.DisplayFor(model=>model.Item2[i].nameProduct)
R$@Html.DisplayFor(model=>model.Item2[i].pricesaleProduct)

@DisplayFor(model=>model.Item2[i].quantityProduct) sum+=Model.Item2[i]。pricesaleProduct*Model.Item2[i]。quantityProduct; } } } 总值:@sum
我想在我的表中显示“sum”,但尝试了几个变体并出现了一个get错误


我该怎么做呢?

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

 double sum = 0;

 @if (Model != null)
 {  

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

出现了什么错误?当前上下文中不存在名称“sum”(在视图顶部声明
sum
(在
标记之前)