public ActionResult Edit(localRegister dataModel) { if (!ModelState.IsValid || dataModel.local.IdDistrict == 0) { if (dataModel.local.IdDistrict == 0) { TempData["Error"] = "Please select a District."; } ViewBag.IdDistrict = new SelectList(db.District, "IdDistrict", "Name"); ViewBag.IdRestaurant = new SelectList(db.Restaurant, "IdRestaurant", "Name"); localRegister model = new localRegister { restaurant = db.Restaurant.Find(dataModel.idRestaurant), cantMesas = db.Table.Count(cantTable => cantTable.IdLocal == dataModel.local.IdLocal), idRestaurant = dataModel.idRestaurant, menu = GetMenuLocalEdit(dataModel.idRestaurant), local = dataModel.local, photos = db.Photo.ToList().Where(m => m.Local.Any(n => n.IdLocal == dataModel.local.IdLocal)), uploadFilesNames = "", deletedFilesIds = "" }; return(View(model)); } using (var dbTran = db.Database.BeginTransaction()) { try { Local local = db.Local.Find(dataModel.local.IdLocal); local.Longitude = dataModel.local.Longitude; local.Latitude = dataModel.local.Latitude; local.IdDistrict = dataModel.local.IdDistrict; local.Detail = dataModel.local.Detail; db.Entry(local).State = EntityState.Modified; if (dataModel.menu != null) { foreach (DishLocal localDish in dataModel.menu) { DishesPerLocal tmpDishesPerLocal = db.DishesPerLocal.Find(local.IdLocal, localDish.idDish); tmpDishesPerLocal.State = localDish.state; db.Entry(tmpDishesPerLocal).State = EntityState.Modified; } } int cantMesas = db.Table.Count(cantTable => cantTable.IdLocal == local.IdLocal); if (dataModel.cantMesas > cantMesas) { for (int i = cantMesas + 1; i < dataModel.cantMesas; i++) { Table tmpTable = new Table(); tmpTable.IdLocal = local.IdLocal; tmpTable.DistinctiveName = i.ToString(); db.Table.Add(tmpTable); } } else if (dataModel.cantMesas < cantMesas) { var tables = db.Table.Where(cantTable => cantTable.IdLocal == local.IdLocal).ToArray(); for (int i = cantMesas - 1; i > dataModel.cantMesas; i--) { db.Table.Remove(tables[i]); } } if (dataModel.deletedFilesIds != null) { foreach (var idPhotoDelete in dataModel.deletedFilesIds.Split(',')) { db.PR_DeleteLocalPhoto(local.IdLocal, int.Parse(idPhotoDelete)); } } if (dataModel.uploadFilesNames != null) { string[] uploadFiles = dataModel.uploadFilesNames.Split(','); for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase tmpFile = Request.Files[i]; if (tmpFile != null && uploadFiles.Any(name => name == tmpFile.FileName) && uploadFiles.Length > 0) { byte[] dbImage = FileUpload(tmpFile); Photo tmpPhoto = new Photo(); tmpPhoto.Photo1 = dbImage; db.Photo.Add(tmpPhoto); local.Photo.Add(tmpPhoto); tmpPhoto.Local.Add(local); uploadFiles = uploadFiles.Where(name => name != tmpFile.FileName).ToArray(); } } } db.SaveChanges(); dbTran.Commit(); TempData["Success"] = "Local edited successfully."; return(RedirectToAction("Index", "Locals", new { id = dataModel.idRestaurant })); } catch (Exception ex) { dbTran.Rollback(); TempData["Error"] = ex.ToString(); return(RedirectToAction("Edit", "Locals", new { id = dataModel.local.IdLocal })); } } }