public void SaveDepartment(Departments dep) { #region Vertification var errors = new NameValueCollection(); errors.Add(GetRuleViolations.checkRequire("CompanyName", "회사명", dep.CompanyName)); errors.Add(GetRuleViolations.checkBetween("CompanyName", "회사명", dep.CompanyName, 2, 50)); errors.Add(GetRuleViolations.checkRequire("DepartmentName", "부서명", dep.DepartmentName)); errors.Add(GetRuleViolations.checkBetween("DepartmentName", "부서명", dep.DepartmentName, 2, 30)); if (errors.Count > 0) throw new RuleException(errors); #endregion if(dep.DepartmentID==null) { dep.DepartmentID = GenerateRandomCode.create_5(); dep.Date = DateTime.Now; context.Departments.Add(dep); } else { context.Entry(dep).State = EntityState.Modified; } context.SaveChanges(); }
public ActionResult edit(Departments dep) { if(ModelState.IsValid) { try { repository.SaveDepartment(dep); } catch(RuleException ex) { ex.CopyToModelState(ModelState); } } if(ModelState.IsValid) { return RedirectToAction("list"); } return View(); }