I made a foreach loop that loops through all cart_items from a specific user. I need to calculate every product total price (product price * quantity) and store that in a viewbag to show on the page itself. I don't know how I can solve this issue though. This is what I've coded (the viewbag gets overrided, so the last row, that's the value it will show):
public ActionResult Index()
{
string user_id = User.Identity.GetUserId();
var order_id = db.Orders.Where(x => x.User_Id == user_id).Where(x => x.Paid == 0).Select(x => x.Order_Id).FirstOrDefault();
var select_user_cart = db.Order_details.Where(x => x.Order_Id == order_id);
var quantity_price = from x in db.Order_details
where x.Order_Id == order_id
select new { x.Quantity, x.Current_price};
List<decimal> Calc = new List<decimal>();
foreach (var item in quantity_price)
{
var quantity = item.Quantity.ToString();
var double_quantity = Convert.ToDecimal(quantity);
var double_price = Convert.ToDecimal(item.Current_price);
var calc = double_quantity * double_price;
Calc.Add(calc);
}
ViewBag.Calc = Calc;
var order_details = db.Order_details.Include(o => o.Product).Include(o => o.Order);
return View(select_user_cart.ToList());
}