public ActionResult DeleteCategory(int id) { using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == id); db.ProductCategories.RemoveRange(new[] { category }); db.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult DeleteProduct(int id) { using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == id); db.Products.RemoveRange(new[] { product }); db.SaveChanges(); return(RedirectToAction("Products")); } }
public ActionResult EditCategory(EditCategoryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == model.CategoryId); category.Name = model.Name; category.CategoryId = model.CategoryId; db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult CreateCategory(CreateCategoryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var category = new ProductCategory { Name = model.Name }; db.ProductCategories.Add(category); db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult EditProduct(ProductEditViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == model.ProductId); product.Name = model.Name; product.Description = model.Description; product.Price = model.Price; product.CategoryId = model.SelectedCategoryId; db.SaveChanges(); } return(RedirectToAction("Products")); }
public ActionResult CreateProduct(ProductCreateProductViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var product = new Product { Name = model.Name, Description = model.Description, Price = model.Price, CategoryId = model.SelectedCategoryId, }; db.Products.Add(product); db.SaveChanges(); } return(RedirectToAction("Products")); }