Reputation: 787
I have a view model having 2 properties: one is a list of items class objects and the other one is an object of order class. I want to show the sum of the price calculated from items' list in order.price
Here is view model:
public class PlaceOrderViewModel
{
public Orders Order { get; set; }
public List<Items> Items { get; set; }
}
Here is items model:
public class Items
{
public long Id { get; set; }
public long OrderId { get; set; }
public int ItemType { get; set; }
public string Detail { get; set; }
public int ItemsCount { get; set; }
public int Price { get; set; }
}
I am looking for something like this,
@Html.DisplayFor(model => model.Order.Price, new { Value = @Model.Items.Sum(s => s.Price)})
I did try this but this doesn't work for me.
I want to bind the sum value with the order.price
property.
Any help?
Upvotes: 0
Views: 2033
Reputation: 2479
Let's say you have this:
public class Order
{
public int Id { get; set; }
public List<Items> Items { get; set; }
...
public int TotalPrice
{
get
{
return Items == null ? 0 : Items.Sum(p => p.Price);
}
}
}
And in your view should have:
@Html.DisplayFor(model => model.Order.TotalPrice)
Upvotes: 1