public ActionResult DeleteAllProductsByCategory(string categoryId) { if (categoryId == null) { // return http error return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var viewModel = new DeleteAllProductsByCategory(); viewModel.products = _db.Products.Where(p => p.CategoryId.ToString() == categoryId); viewModel.CategoryId = Convert.ToInt32(categoryId); return(View(viewModel)); }
public ActionResult DeleteAllProductsByCategory([Bind(Include = "CategoryId")] DeleteAllProductsByCategory viewModel) { var categoryId = Request["CategoryId"]; // if model state is valid if (ModelState.IsValid) { // for each product in the list of products for deletion? // should i actually edit them an set a flag so they are not shown foreach (Product product in _db.Products.Where(p => p.CategoryId.ToString() == categoryId)) { //_db.Products.Remove(product); product.IsDeleted = true; _db.Entry(product).State = EntityState.Modified; } _db.SaveChanges(); // return to the index page return(RedirectToAction("Index")); } return(View(viewModel)); }