2015-08-23 177 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

謝謝您的答覆。它像一個魅力。我現在需要做的是在底部的客戶發票總額中不斷累計總額,併爲此增加增值稅,也可能會計算物品數量。我將如何做到這一點?它會在桌子上嗎? –

相關問題