public ActionResult Edit(RegisterRestaurantModel model, HttpPostedFileBase image) { Restaurant dbRest = db.Restaurant.Find(Session["RestId"]); if (ModelState.IsValid) { if (model.restTypesId != null && model.restTypesId.Any()) { String typeList = ""; foreach (var typeId in model.restTypesId) { typeList += typeId + ","; } db.PR_UpdateRestaurantTypes(model.restaurant.IdRestaurant, typeList.Remove(typeList.Length - 1)); } else { db.PR_DeleteRestaurantTypes(model.restaurant.IdRestaurant); } if (image != null && image.ContentLength > 0) { byte[] dbImage = FileUpload(image); dbRest.Logo = dbImage; } dbRest.Name = model.restaurant.Name; db.Entry(dbRest).State = EntityState.Modified; db.SaveChanges(); TempData["Success"] = dbRest.Name + " edited successfully."; return(RedirectToAction("Index")); } model.selectedItems = new MultiSelectList(db.Type, "IdType", "Name", dbRest.Type.Select(t => t.IdType)); return(View(model)); }
public ActionResult Create(RegisterRestaurantModel model, HttpPostedFileBase image) { if (ModelState.IsValid) { Restaurant restaurant = model.restaurant; if (model.restTypesId != null) { foreach (var typeId in model.restTypesId) { Models.Type restType = db.Type.Find(typeId); restType.Restaurant.Add(restaurant); restaurant.Type.Add(restType); } } if (image != null) { byte[] dbImage = FileUpload(image); restaurant.Logo = dbImage; } var identity = (System.Web.HttpContext.Current.User as MyIdentity.MyPrincipal).Identity as MyIdentity; restaurant.IdAdmin = identity.User.IdCard; db.Restaurant.Add(restaurant); db.SaveChanges(); TempData["Success"] = restaurant.Name + " created successfully."; return(RedirectToAction("Index")); } ViewBag.Type = new SelectList(db.Type, "IdType", "Name"); return(View(model)); }
// GET: Restaurants/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Restaurant restaurant = db.Restaurant.Find(id); if (restaurant == null) { return(HttpNotFound()); } Session["RestId"] = id; RegisterRestaurantModel model = new RegisterRestaurantModel { restaurant = restaurant }; model.selectedItems = new MultiSelectList(db.Type, "IdType", "Name", restaurant.Type.Select(t => t.IdType)); db.PR_DeleteUnusedTypes(); return(View(model)); }