public ActionResult Create(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(); model.restaurant = db.Restaurant.Find(dataModel.idRestaurant); model.idRestaurant = dataModel.idRestaurant; model.menu = GetMenuLocal(dataModel.idRestaurant); model.uploadFilesNames = String.Empty; model.local = new Local(); return(View(model)); } using (var dbTran = db.Database.BeginTransaction()) { try { int idLocal = Convert.ToInt32(db.PR_CreateLocal(dataModel.idRestaurant, dataModel.local.IdDistrict, dataModel.local.Detail, dataModel.local.Latitude, dataModel.local.Longitude).ToList()[0]); if (dataModel.menu != null) { foreach (DishLocal localDish in dataModel.menu) { DishesPerLocal tmpDishesPerLocal = new DishesPerLocal(); tmpDishesPerLocal.IdLocal = idLocal; tmpDishesPerLocal.IdDish = localDish.idDish; tmpDishesPerLocal.State = localDish.state; db.DishesPerLocal.Add(tmpDishesPerLocal); } } Local tmpLocal = db.Local.Find(idLocal); for (int i = 1; i <= dataModel.cantMesas; i++) { Table tmpTable = new Table(); tmpTable.IdLocal = tmpLocal.IdLocal; tmpTable.DistinctiveName = i.ToString(); db.Table.Add(tmpTable); } 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); tmpLocal.Photo.Add(tmpPhoto); tmpPhoto.Local.Add(tmpLocal); uploadFiles = uploadFiles.Where(name => name != tmpFile.FileName).ToArray(); } } } db.SaveChanges(); dbTran.Commit(); TempData["Success"] = "Local created successfully"; return(RedirectToAction("Index", "Locals", new { id = dataModel.idRestaurant })); } catch (Exception ex) { dbTran.Rollback(); TempData["Error"] = ex.ToString(); return(RedirectToAction("Create", "Locals", new { id = dataModel.idRestaurant })); } } }