public ActionResult Edit(int ID) { try { var requestGroupEditViewModel = new RequestGroupEditViewModel(); if (ID > 0) { REF_REQUEST_GROUP_TB requestGroup = _uow.Repository <REF_REQUEST_GROUP_TB>().GetById(ID); requestGroupEditViewModel.RequestGroupDescription = requestGroup.SZ_DESCRIPTION == null ? "" : requestGroup.SZ_DESCRIPTION; requestGroupEditViewModel.RequestGroupID = requestGroup.N_REQUEST_GROUP_SYSID; requestGroupEditViewModel.ShowMessageDescription = false; requestGroupEditViewModel.MessageDescription = ""; } else { requestGroupEditViewModel.RequestGroupID = ID; requestGroupEditViewModel.RequestGroupDescription = ""; requestGroupEditViewModel.ShowMessageDescription = false; requestGroupEditViewModel.MessageDescription = ""; } return(View("Edit", requestGroupEditViewModel)); } catch (Exception ex) { if (ex.InnerException == null) { ViewBag.Message = "Function: RequestGroupController.Edit_GET\n\nError: " + ex.Message; } else { ViewBag.Message = "Function: RequestGroupController.Edit_GET\n\nError: " + (ex.Message + "\n\nInnerException: " + ex.InnerException.Message); }; Session["ErrorMessage"] = ViewBag.Message; return(RedirectToAction("InternalServerError", "Error")); }; }
public ActionResult Edit(RequestGroupEditViewModel Model) { bool saveFailed; do { saveFailed = false; try { if (_requestGroupRepo.IsRequestGroupDescriptionDuplicate(Model.RequestGroupDescription)) { Model.ShowMessageDescription = true; Model.MessageDescription = "The Request Group Description already exists. Please enter a different one."; return(View(Model)); } else { if (ModelState.IsValid) { int id = 0; if (Model.RequestGroupID == 0) //new { REF_REQUEST_GROUP_TB requestGroup = new REF_REQUEST_GROUP_TB() { B_INACTIVE = false, DT_ENTERED = DateTime.UtcNow, DT_MODIFIED = DateTime.UtcNow, SZ_ENTERED_BY = _modifiedBy, SZ_MODIFIED_BY = _modifiedBy, SZ_DESCRIPTION = Model.RequestGroupDescription }; _uow.Repository <REF_REQUEST_GROUP_TB>().Add(requestGroup); _uow.SaveChanges(); REF_REQUEST_GROUP_TB requestGroupFound = _uow.Repository <REF_REQUEST_GROUP_TB>().Find(u => u.SZ_ENTERED_BY == _modifiedBy && u.SZ_MODIFIED_BY == _modifiedBy && u.SZ_DESCRIPTION == Model.RequestGroupDescription ).FirstOrDefault(); id = requestGroupFound.N_REQUEST_GROUP_SYSID; } else //edit { REF_REQUEST_GROUP_TB requestGroup = _uow.Repository <REF_REQUEST_GROUP_TB>().GetById(Model.RequestGroupID); requestGroup.DT_MODIFIED = DateTime.UtcNow; requestGroup.SZ_DESCRIPTION = Model.RequestGroupDescription; requestGroup.SZ_MODIFIED_BY = _modifiedBy; _uow.Repository <REF_REQUEST_GROUP_TB>().Update(requestGroup); _uow.SaveChanges(); id = Model.RequestGroupID; } return(RedirectToAction("Index", new { ID = id })); } return(View(Model)); } } catch (DbEntityValidationException ex) { StringBuilder sb = new StringBuilder(); foreach (var failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation:\n\n", failure.Entry.Entity.GetType()); foreach (var error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } if (ex.InnerException == null) { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message + "\n\n" + sb; } else { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message + "\n\n" + sb + "\n\n" + ex.InnerException.Message; }; } catch (DbUpdateConcurrencyException ex) { saveFailed = true; var entry = ex.Entries.Single(); entry.OriginalValues.SetValues(entry.GetDatabaseValues()); } catch (DataException ex) { if (ex.InnerException == null) { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message + "\n\nBaseException: " + ex.GetBaseException().Message; } else { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message + "\n\nBaseException: " + ex.GetBaseException().Message + "\n\nInnerException: " + ex.InnerException.Message; }; } catch (Exception ex) { if (ex.InnerException == null) { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message; } else { ViewBag.Message = "Function: RequestGroupController.Edit_POST\n\nError: " + ex.Message + "\n\nBaseException: " + ex.InnerException.Message; }; }; } while (saveFailed); Session["ErrorMessage"] = ViewBag.Message; return(RedirectToAction("InternalServerError", "Error")); }