计算在剃刀中无法正确显示

时间:2015-03-30 09:51:33

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

我是asp.net MVC的新手,对Razor View有疑问。

我有一些问题显示计算结果

我有以下代码:

 foreach (var item in Model.Items)
        {
            <tr class="ourProduct">
                <td>
                    <div class="ourProductDesc">
                        <h4>
                            @item.Description
                        </h4>

                        <div class="ourPrice">
                            <span>£ @item.price</span>
                        </div>
                    </div>
                </td>

                <td class="ourPrice">
                    £ @item.Quantity * item.price
                </td>
            </tr>
        }

如果有人能告诉我如何进行此计算并按预期显示结果,我将非常感激。目前我的输出结果不正确:

£1 * item.Prod.price

2 个答案:

答案 0 :(得分:4)

你几乎就在那里 - 你只需将计算包装在()

另外,在编写HTML而不是写入时,您应该使用“&amp; pound” - 有许多特殊字符和代码 - 这是一个有用的网站,用于列出字符和代码:http://character-code.com/

回到原点问题:

将您的代码更新为以下内容,它应该按预期工作:

foreach (var item in Model.Items)
        {
            <tr class="ourProduct">
                <td>
                    <div class="ourProductDesc">
                        <h4>
                            @item.Description
                        </h4>

                        <div class="ourPrice">
                            <span>&pound; @item.price</span>
                        </div>
                    </div>
                </td>

                <td class="ourPrice">
                    &pound; @(item.Quantity * item.price)
                </td>
            </tr>
        }

答案 1 :(得分:1)

将剃刀代码计算部分包含在@()内,如下所示: -

@foreach (var item in Model.Items)
    {
        <tr class="ourProduct">
            <td>
                <div class="ourProductDesc">
                    <h4>
                        @item.Description
                    </h4>

                    <div class="ourPrice">
                        <span>£ @item.price</span>
                    </div>
                </div>
            </td>

            <td class="ourPrice">
                £ @(item.Quantity * item.price) // correct here
            </td>
        </tr>
  }

而不是£,您可以使用&pound