public ActionResult DeleteConfirmed(ViewTasteDelete model) { ShopProductsTaste taste = db.ShopProductsTastes .Include(b => b.ShopProducts) .Where(b => b.Id == model.Id) .SingleOrDefault(); if (model.DeleteAll) { if (taste.ShopProducts != null) { foreach (ShopProduct product in taste.ShopProducts) { List <ShopProductsPrice> removePrices = db.ShopProductsPrices.Where(p => p.ShopProduct.Id == product.Id).ToList(); db.ShopProductsPrices.RemoveRange(removePrices); db.SaveChanges(); string dirPath = HttpContext.Server.MapPath("~/Content/Images/Shop/Products"); product.PhotoName = Image.Delete(dirPath, product.PhotoName); db.Entry(product).State = EntityState.Modified; db.SaveChanges(); } db.ShopProducts.RemoveRange(taste.ShopProducts); db.SaveChanges(); } } db.ShopProductsTastes.Remove(taste); db.SaveChanges(); return(RedirectToAction("Index")); }
//========================================================== //========================================================== // GET: AdminPanel/Tastes/Delete/5 public ActionResult Delete(int?id) { ShopProductsTaste taste = db.ShopProductsTastes .Include(b => b.ShopProducts) .Where(b => b.Id == id) .SingleOrDefault(); if (taste == null) { return(HttpNotFound()); } ViewTasteDelete model = new ViewTasteDelete { Id = taste.Id, Name = taste.Name, Products = taste.ShopProducts, ProductsCount = taste.ShopProducts.Count, DeleteAll = false }; return(View(model)); }