public ActionResult Edit([Bind(Include = "Id,Name,CategoryId,Price, Image")] MenuItemSaveViewModel menuItem) { if (ModelState.IsValid) { var item = _db.MenuItems.Find(menuItem.Id); if (item == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } item.Name = menuItem.Name; item.Price = menuItem.Price; item.CategoryId = menuItem.CategoryId; if (menuItem.Image != null && menuItem.Image.ContentLength > 0) { item.Image = ImageUploader.Upload(menuItem.Image); } _db.Entry(item).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(_db.Categories, "Id", "Name", menuItem.CategoryId); return(View(menuItem)); }
public ActionResult Create([Bind(Include = "Id,Name,CategoryId,Price,Image")] MenuItemSaveViewModel menuItem) { if (menuItem.Image == null || menuItem.Image.ContentLength == 0) { ModelState.AddModelError("Image", "Please select an image to upload."); } else if (!ImageUploader.IsValidImageType(menuItem.Image)) { ModelState.AddModelError("Image", "The file you are attempting to upload is not an image."); } if (ModelState.IsValid) { var imageUrl = ImageUploader.Upload(menuItem.Image); _db.MenuItems.Add(new MenuItem() { Name = menuItem.Name, Price = menuItem.Price, CategoryId = menuItem.CategoryId, Image = imageUrl }); _db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryId = new SelectList(_db.Categories, "Id", "Name", menuItem.CategoryId); return(View(menuItem)); }