public ActionResult EditEmployee(EditEmployeeModel model) { // check if any of the dependents are not containign a first or last name. if (model.Dependents != null && model.Dependents.Any(x => string.IsNullOrEmpty(x.DependentFirstName) || string.IsNullOrEmpty(x.DependentLastName))) { ModelState.AddModelError("", "Depedents First and Last name are required."); } if (ModelState.IsValid) { Employee employee = EmployeeRepository.GetById(model.Id); employee.FirstName = model.FirstName; employee.LastName = model.LastName; employee.StateId = model.StateId.Value; employee.Email = model.Email; employee.BenefitSettingId = model.SelectedBenefitSettingId; if (model.Dependents != null && model.Dependents.Any()) { DependentRepository.Delete(DependentRepository.GetByEmployeeId(employee.Id).Where(x => model.Dependents.All(m => m.DependentId != x.Id))); foreach (var dependent in model.Dependents) { Dependent dependentEntity = DependentRepository.GetById(dependent.DependentId); if (dependentEntity != null) { dependentEntity.FirstName = dependent.DependentFirstName; dependentEntity.LastName = dependent.DependentLastName; DependentRepository.Update(dependentEntity); } else { employee.Dependents.Add(new Dependent { EmployeeId = model.Id, FirstName = dependent.DependentFirstName, LastName = dependent.DependentLastName }); } } } else { DependentRepository.Delete(DependentRepository.GetByEmployeeId(employee.Id)); } employee = EmployeeRepository.Update(employee); return(Json(new PostResultModel { IsSuccess = true, Message = "Employee Updated", ItemId = employee.Id, Html = PartialView("EmployeeRow", new EmployeeRowModel(employee)).RenderToString() })); } model.Populate(StateRepository.GetAll(), BenefitSettingsRepository.GetPublic()); return(PartialView(model)); }