public JsonResult AddCart(int itemId) { var order = CacheHelper._CacheHelper.GetCartFromSession(); OrderDetailModel toUpdate; if(order.OrderDetails.Exists(i => i.ItemId == itemId)) { toUpdate = order.OrderDetails.SingleOrDefault(i => i.ItemId == itemId); toUpdate.Quantity += 1; }else{ toUpdate = new OrderDetailModel(Context.GetItem(itemId)); } order.AddItem(toUpdate); return Json(new { cartHtml = RenderRazorViewToString("_CartPartial", new CartViewModel()), itemCount = order.OrderDetails.Sum(i => i.Quantity) }); }
public void AddItem(OrderDetailModel item) { item.Quantity = item.Quantity <= 0 ? 1 : item.Quantity; if (OrderDetails.Exists(dt => dt.ItemId == item.ItemId)) { var toUpdate = OrderDetails.SingleOrDefault(dt => dt.ItemId == item.ItemId); toUpdate.Quantity = item.Quantity; } else { OrderDetails.Add(item); } CalculateTotal(); }