// GET: ProductCategoryManage/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ProductCategory productCategory = db.ProductCategories.Find(id); if (productCategory == null) { return HttpNotFound(); } var vm = new ProductCategoryManageDeleteViewModel { Id = productCategory.Id, Name = productCategory.Name, Description = productCategory.Description, UrlSlug = productCategory.UrlSlug }; return View(vm); }
public ActionResult DeleteConfirmed(int id) { var productCategory = db.ProductCategories.Find(id); var productsHaveTheCateogory = db.Products.Where(p => p.CategoryId == id); if(productsHaveTheCateogory.Count()!=0) { ModelState.AddModelError("", "在删除分类之前,请先更改或者删除拥有该分类商品的商品分类"); var vm = new ProductCategoryManageDeleteViewModel { Id = productCategory.Id, Name = productCategory.Name, Description = productCategory.Description, UrlSlug = productCategory.UrlSlug }; return View(vm); } db.ProductCategories.Remove(productCategory); db.SaveChanges(); return RedirectToAction("Index"); }