public ActionResult Create(Payment payment) { if (ModelState.IsValid) { db.Payments.Add(payment); var responseSave = DBHelper.SaveChanges(db); if (responseSave.Succeeded) { if (payment.PaymentFile != null) { const string folder = "~/Content/Payments"; var file = string.Format("{0}.jpg", payment.PaymentId); var response = FilesHelper.UploadPhoto(payment.PaymentFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); payment.Voucher = pic; db.Entry(payment).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } ModelState.AddModelError(string.Empty, responseSave.Message); } ViewBag.ProjectId = new SelectList(CombosHelper.GetProjects(), "ProjectId", "Name", payment.ProjectId); return(View(payment)); }
public ActionResult Create(User user) { if (ModelState.IsValid) { db.Users.Add(user); var responseSave = DBHelper.SaveChanges(db); if (responseSave.Succeeded) { UsersHelper.CreateUserASP(user.UserName, "User"); if (user.PhotoFile != null) { const string folder = "~/Content/Users"; var file = string.Format("{0}.jpg", user.UserId); var response = FilesHelper.UploadPhoto(user.PhotoFile, folder, file); if (response) { var pic = string.Format("{0}/{1}", folder, file); user.Photo = pic; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index")); } ModelState.AddModelError(string.Empty, responseSave.Message); } ViewBag.CityId = new SelectList(CombosHelper.GetCities(user.DepartmentId), "CityId", "Name", user.CityId); ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartments(), "DepartmentId", "Name", user.DepartmentId); var adminUser = WebConfigurationManager.AppSettings["AdminUser"]; if (adminUser == User.Identity.Name) { ViewBag.UserRolId = new SelectList(CombosHelper.GetUserRols(), "UserRolId", "Name", user.UserRolId); } else { var userIdentity = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); ViewBag.UserRolId = new SelectList(CombosHelper.GetUserRols(), "UserRolId", "Name", user.UserRolId); } return(View(user)); }
public static Response SaveChanges(MyLinkContext db) { try { db.SaveChanges(); return(new Response { Succeeded = true, }); } catch (Exception ex) { var response = new Response { Succeeded = false, }; if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("_Index")) { response.Message = "There are a record with the same value."; } else if (ex.InnerException != null && ex.InnerException.InnerException != null && ex.InnerException.InnerException.Message.Contains("REFERENCE")) { response.Message = "The record can't be delete because it has related records."; } else { response.Message = ex.Message; } return(response); } }