public ActionResult Index(int ShoeId, int CategoryId) { if (ShoeId == 0) { return(HttpNotFound()); } if (ModelState.IsValid && CategoryId > 0) { ShoeItem product = db.ShoeItems.SingleOrDefault(p => p.ID == ShoeId); if (product == null) { return(HttpNotFound()); } //Save ShoeCategories category = db.ShoeCategories.Where(pc => pc.ID == CategoryId).SingleOrDefault(); if (category == null) { return(HttpNotFound("Category not found")); } product.Categories.Add(category); db.SaveChanges(); } AssignCategoryViewModel model = CreateAssignCategoryViewModel(ShoeId); return(View(model)); }
public ActionResult Create([Bind(Include = "ID,ShoeName,Description,Price,Enabled,StockCount,LastOrderDate,LastModified,LastModifiedBy,Comments,ShoeTypeId")] ProductViewModel shoeProdVM) { if (ModelState.IsValid) { ShoeItem p = new ShoeItem() { ID = shoeProdVM.ID, ShoeName = shoeProdVM.ShoeName, Description = shoeProdVM.Description, Price = shoeProdVM.Price, Enabled = shoeProdVM.Enabled, StockCount = shoeProdVM.StockCount, LastOrderDate = shoeProdVM.LastOrderDate, LastModified = shoeProdVM.LastModified, LastModifiedBy = shoeProdVM.LastModifiedBy, Comments = shoeProdVM.Comments, ShoeTypeId = shoeProdVM.ShoeTypeId, ShoeTypes = shoeProdVM.ShoeTypes, Categories = db.ShoeCategories.Where(c => shoeProdVM.SelectedCategoryIds.Contains(c.ID)).ToList() }; db.ShoeItems.Add(p); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ShoeTypeId = new SelectList(db.ShoeTypes, "ID", "TypeName", shoeProdVM.ShoeTypeId); return(View(shoeProdVM)); }
public ActionResult DeleteConfirmed(int id) { ShoeItem shoeItem = db.ShoeItems.Find(id); db.ShoeItems.Remove(shoeItem); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: ShoeItems/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ShoeItem shoeItem = db.ShoeItems.Find(id); if (shoeItem == null) { return(HttpNotFound()); } return(View(shoeItem)); }
public ActionResult Edit([Bind(Include = "ID,ShoeName,Description,Price,Enabled,StockCount,LastOrderDate,LastModified,LastModifiedBy,Comments,ShoeTypeId")] ProductViewModel shoeProdVM) { if (ModelState.IsValid) { ShoeItem existingProduct = db.ShoeItems.Find(shoeProdVM.ID); //edit the product existingProduct.ShoeName = shoeProdVM.ShoeName; existingProduct.Description = shoeProdVM.Description; existingProduct.Price = shoeProdVM.Price; existingProduct.Enabled = shoeProdVM.Enabled; existingProduct.StockCount = shoeProdVM.StockCount; existingProduct.LastOrderDate = shoeProdVM.LastOrderDate; existingProduct.LastModified = shoeProdVM.LastModified; existingProduct.LastModifiedBy = shoeProdVM.LastModifiedBy; existingProduct.Comments = shoeProdVM.Comments; existingProduct.ShoeTypeId = shoeProdVM.ShoeTypeId; existingProduct.ShoeTypes = shoeProdVM.ShoeTypes; existingProduct.Categories = shoeProdVM.Categories; //get a list of existing category ids from database var existingProductCategoryIds = existingProduct.Categories.Select(c => c.ID).ToList(); //Find ids of the deleted categories from the product's Categories collection by the product's existing categoriy ids minus current category ids from the view var deletedCategories = existingProductCategoryIds.Except(shoeProdVM.SelectedCategoryIds).ToList(); //Find ids of the added categories to the current product's Category id list from the view minus the product's existing category ids (from the db) var addedCategories = shoeProdVM.SelectedCategoryIds.Except(existingProductCategoryIds).ToList(); //remove deleted categories from the products' categories collection foreach (int id in deletedCategories) { var category = db.ShoeCategories.Where(c => c.ID == id).FirstOrDefault(); existingProduct.Categories.Remove(category); } //add the new categories to the products' categories collection foreach (int id in addedCategories) { var category = db.ShoeCategories.Where(c => c.ID == id).FirstOrDefault(); // var category = db.Categories.FirstOrDefault(c => c.ID == id); this is another way to write the line above existingProduct.Categories.Add(category); } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ShoeTypeId = new SelectList(db.ShoeTypes, "ID", "TypeName", shoeProdVM.ShoeTypeId); return(View(shoeProdVM)); }
private AssignCategoryViewModel CreateAssignCategoryViewModel(int ShoeId) { AssignCategoryViewModel model = new AssignCategoryViewModel(); ShoeItem product = db.ShoeItems .Where(p => p.ID == ShoeId) .FirstOrDefault(); if (product == null) { model.ShoeId = 0; } else { model.ShoeId = product.ID; model.ShoeName = product.ShoeName; //Get the list of assigned category ids for this product IList <int> assignedCategoryIds = product.Categories.Select(c => c.ID).ToList(); //Get a list of assigned category objects IList <ShoeCategories> assignedCategories = db.ShoeCategories.Where(c => assignedCategoryIds.Contains(c.ID)).ToList(); model.AssociatedCategories = assignedCategories; //Get a list of available category objects that could be assigned IList <ShoeCategories> availableCategories = db.ShoeCategories.Where(c => !assignedCategoryIds.Contains(c.ID)).ToList(); availableCategories.Insert(0, new ShoeCategories() { ID = 0, CategoryName = "--- Select a Category ---" }); //Convert to SelectList object for use with (bind to) drop down list. model.Categories = new SelectList(availableCategories, "ID", "CategoryName", 0); } return(model); }
// GET: AssignCategory/Delete/5 public ActionResult Delete(int CategoryId, int ShoeId) { if (CategoryId > 0) { ShoeCategories category = db.ShoeCategories.Where(pc => pc.ID == CategoryId).SingleOrDefault(); if (category == null) { return(HttpNotFound()); } if (ShoeId > 0) { ShoeItem product = db.ShoeItems.Where(p => p.ID == ShoeId).SingleOrDefault(); if (product == null) { return(HttpNotFound()); } product.Categories.Remove(category); db.SaveChanges(); } } return(View("Index", CreateAssignCategoryViewModel(ShoeId))); }
// GET: ShoeItems/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ShoeItem shoeItem = db.ShoeItems.Find(id); if (shoeItem == null) { return(HttpNotFound()); } var model = new ProductViewModel() { ID = shoeItem.ID, ShoeName = shoeItem.ShoeName, Description = shoeItem.Description, Price = shoeItem.Price, Enabled = shoeItem.Enabled, StockCount = shoeItem.StockCount, LastOrderDate = shoeItem.LastOrderDate, LastModified = shoeItem.LastModified, LastModifiedBy = shoeItem.LastModifiedBy, Comments = shoeItem.Comments, ShoeTypeId = shoeItem.ShoeTypeId, ShoeTypes = shoeItem.ShoeTypes, Categories = shoeItem.Categories, SelectedCategoryIds = shoeItem.Categories.Select(c => c.ID).ToList() }; ViewBag.ShoeTypeId = new SelectList(db.ShoeTypes, "ID", "TypeName", shoeItem.ShoeTypeId); model.AllCategories = db.ShoeCategories.Select(c => new SelectListItem { Text = c.CategoryName, Value = c.ID.ToString() }).ToList(); return(View(shoeItem)); }