我做了一个 foreach 循环,循环遍历来自特定用户的所有 car_items。我需要计算每个产品的总价(产品价格 * 数量)并将其存储在查看袋中以显示在页面本身上。我不知道如何解决这个问题。这是我编码的内容(viewbag 被覆盖,所以最后一行,这就是它将显示的值):
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());
}
手掌心
慕无忌1623718
慕尼黑5688855
相关分类