public ActionResult DeleteConfirmed(ViewBrandDelete model) { ShopProductsBrand brand = db.ShopProductsBrands .Include(b => b.ShopProducts) .Where(b => b.Id == model.Id) .SingleOrDefault(); if (model.DeleteAll) { if (brand.ShopProducts != null) { foreach (ShopProduct product in brand.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(brand.ShopProducts); db.SaveChanges(); } } db.ShopProductsBrands.Remove(brand); db.SaveChanges(); return(RedirectToAction("Index")); }
//========================================================== //========================================================== public ActionResult Delete(int?id) { ShopProductsBrand brand = db.ShopProductsBrands .Include(b => b.ShopProducts) .Where(b => b.Id == id) .SingleOrDefault(); if (brand == null) { return(HttpNotFound()); } ViewBrandDelete model = new ViewBrandDelete { Id = brand.Id, Name = brand.Name, Products = brand.ShopProducts, ProductsCount = brand.ShopProducts.Count, DeleteAll = false }; return(View(model)); }