public ActionResult Create(Product product, string[] ImageGallery) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.Products.Add(product); int kq = db.SaveChanges(); if (kq > 0) { foreach (var item in ImageGallery) { var image = new Image { ImageUrl = item, ProductID = product.ProductsID, ImageID=1 }; db.Images.Add(image); // db.Entry(image).State = EntityState.Modified; db.SaveChanges(); } } //var image = new Image { ImageUrl = "Hihe", ProductID = product.ProductsID, ImageID=1 }; //db.Images.Add(image); // //db.Entry(image).State = EntityState.Modified; //db.SaveChanges(); return RedirectToAction("Index"); } ModelState.AddModelError("Thông báo", "Có lỗi"); ViewBag.BrandID = new SelectList(db.Brands, "BrandID", "Name", product.BrandID); ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID); return View(product); }
// Vừa them, vua cap nhat so luong public void AddToCart(Product p, int quantity) { var product = MyCarts.Where(x => x.Product.ProductsID == p.ProductsID).FirstOrDefault(); if (product == null) { MyCarts.Add(new CartItem { Product = p, Quantity= quantity}); } else { product.Quantity += quantity; } }
public void RemoveFromCart(Product p) { MyCarts.RemoveAll(x => x.Product.ProductsID.Equals(p.ProductsID)); }