计算未在 Razor 中正确显示

Calculation Not Displaying Correctly In 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

将剃须刀代码计算部分放在@()内,如图所示:-

@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.

而不是 £

大功告成 - 您只需将计算包含在 ( )

此外,在写 HTML 而不是写 £ 时,您应该使用“£” - 有很多特殊字符和代码 - 这是一个提供字符和代码列表的有用网站: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>
        }