public void AddToCart(Product product) { // Get the matching cart and product instances var cartItem = _cartAppService.Find( c => c.CartId == ShoppingCartId && c.ProductId == product.ProductId).SingleOrDefault(); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new Cart { ProductId = product.ProductId, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; _cartAppService.Create(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } }
public ActionResult Create(Product product) { if (ModelState.IsValid) { var validationResult = _productAppService.Create(product); if (validationResult.IsValid) return RedirectToAction("Index"); foreach (var error in validationResult.Errors) ModelState.AddModelError("", error.Message); } ViewBag.CategoryId = new SelectList(_categoryAppService.All(@readonly: true), "CategoryId", "Name", product.CategoryId); ViewBag.OwnerId = new SelectList(_ownerAppService.All(@readonly: true), "OwnerId", "Name", product.OwnerId); return View(product); }