2015-08-23 176 views
1

我有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> 

回答

0

考虑在具有附加属性您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> 
+0

谢谢您的答复。它像一个魅力。我现在需要做的是在底部的客户发票总额中不断累计总额,并为此增加增值税,也可能会计算物品数量。我将如何做到这一点?它会在桌子上吗? –

相关问题