【问题标题】:How to multiply two columns in a table together to produce a new column如何将表中的两列相乘以生成新列
【发布时间】:2017-12-26 23:38:45
【问题描述】:

我有 2 个字段数量和价格。我基本上想将它们相乘并得到另一个名为 Price 的列的值(这是乘法的总和)。

我尝试过使用下面的 html 代码:

@Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)

这是我的表格行代码:

           <table class="table table-striped table-advance table-hover">
                <tbody>
                    <tr>
                        <th><i class="icon_pin_alt"></i> Item Description</th>
                        <th><i class="icon_pin_alt"></i> Quantity</th>
                        <th><i class="icon_calendar"></i> Price</th>
                    </tr>

                    @foreach (var item in Model.ITEM_ORDER)
                    {
                        <tr>
                            <td style="width:auto">
                                @Html.DisplayFor(modelItem => item.ITEM.item_description)
                            </td>
                            <td style="width:auto">
                                @Html.DisplayFor(modelItem => item.item_order_quantity)
                            </td>
                            <td style="width:auto">
                                @Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
                            </td>
                            <td>
                                <div class="btn-group">
                                    @Html.ActionLink("View", "Details", new { id = item.OrderID }) |
                                    @Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |

                                    @Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

【问题讨论】:

    标签: html-table rows


    【解决方案1】:

    考虑在您的viewmodelModel 中添加一个可以为您完成此任务的属性。然后,您可以将其与 html 助手绑定,就像对其他所有字段所做的那样。

    public class YourViewModel
        {
            public int Field1{ get; set; }
            public int Field2{ get; set; }
            public int CalculatedField{ 
                      get {return Field1*Field2;}
                }
        }
    

    或者尝试下面的代码计算值并存储在变量中,然后直接从变量中渲染值。

    试试这个

    <table class="table table-striped table-advance table-hover">
                    <tbody>
                        <tr>
                            <th><i class="icon_pin_alt"></i> Item Description</th>
                            <th><i class="icon_pin_alt"></i> Quantity</th>
                            <th><i class="icon_calendar"></i> Price</th>
                        </tr>
    
                        @foreach (var item in Model.ITEM_ORDER)
                        {
                           var computedValue = item.item_order_quantity*item.ITEM.item_price
                            <tr>
                                <td style="width:auto">
                                    @Html.DisplayFor(modelItem => item.ITEM.item_description)
                                </td>
                                <td style="width:auto">
                                    @Html.DisplayFor(modelItem => item.item_order_quantity)
                                </td>
                                <td style="width:auto">
                                    @(computedValue)
                                </td>
                                <td>
                                    <div class="btn-group">
                                        @Html.ActionLink("View", "Details", new { id = item.OrderID }) |
                                        @Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
    
                                        @Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
                                    </div>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
    

    【讨论】:

    • 感谢您的回复。它就像一个魅力。我现在需要做的是不断地在底部有一个客户发票的总和,并在其中添加增值税,也许还要计算项目的数量。我该怎么做?它会在桌子上吗?
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多