Mann
Mann

Reputation: 17

Sum values from two columns in View MVC

I would like to sum values I get from two columns, these values are added from the data collected in View page based on customers so adding it in a controller from database is not an option as far as I know, I could be wrong Any help would be appreciated.

I can get sum of two columns individually but can't get sum of two columns.

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.PkgBasePrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BasePrice)
        </th>
    </tr>

    @foreach (var item in Model.Where(ModelItem => 
ModelItem.CustomerId.Equals(Session["CustomerId"])))
    {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.PkgBasePrice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BasePrice)
        </td>

    </tr>
    }

</table>
<h2>
    Package Total: [email protected](c => 
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice)

</h2>
<h2>
    Product Total: [email protected](c => 
c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice)

</h2>

I would like to get sum of PkgBasePrice and BasePrice in one amount

Upvotes: 1

Views: 2031

Answers (1)

GregH
GregH

Reputation: 5459

Since you already have the two individual sums you want to combine, you can add the two sums you have together as follows:

@(@Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.PkgBasePrice) + 
    @Model.Where(c => c.CustomerId.Equals(Session["CustomerId"])).Sum(b => b.BasePrice))

Upvotes: 1

Related Questions