1

I have 2 fields quantity and price. I basically want to multiply them together and get a value for another column called Price (Which is the total of the multiplication).

I have tried using the below html code:

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

This is my Table row code:

           <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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michael Fung
  • 129
  • 13

1 Answers1

0

Consider having an additional property in your viewmodel or Model which does this task for you. Then you can bind this with html helper as you did for every other field.

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

Or try below code which calculates the value and stores in variable, then renders value directly from variable.

try this

<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>
Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
  • Thank you for your response. It worked like a charm. What I need to do now is constantly have a over all total for the customer invoice at the bottom and add VAT to this and maybe count the number of items as well. How would i do this? Would it be in the table? – Michael Fung Aug 23 '15 at 08:23