public ActionResult CreateOrUpdate(Employee employee) { this.employeeTasks.CreateOrUpdate(employee); if (employee.IsValid()) { this.TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "The employee was successfully updated."; return this.RedirectToAction("Index"); } var viewModel = new EmployeeFormViewModel { AvailableTerritories = this.territoryTasks.GetTerritories(), Employee = employee }; return View(viewModel); }
public void CreateOrUpdate(Employee employee) { var employeeToUpdate = employee.Clone(); employee.Territories.Clear(); foreach (var territory in employeeToUpdate.Territories) { employee.Territories.Add(territory); } if (employee.IsValid()) { this.employeeRepository.DbContext.BeginTransaction(); this.employeeRepository.SaveOrUpdate(employee); this.employeeRepository.DbContext.CommitChanges(); } else { this.employeeRepository.DbContext.RollbackTransaction(); } }
public void RiaCreateOrUpdate(Employee employee, string availableTerritories) { employee.Territories.Clear(); foreach (var territory in availableTerritories.Split(',')) { // Depending on how you're accepting user input, better to use NHSearch var hydratedTerritory = this.territoryRepository.GetAll().Where(x => x.Description.Trim() == territory.Trim()).FirstOrDefault(); if (hydratedTerritory != null) { employee.Territories.Add(hydratedTerritory); } } // Currently crashes on duplicates, Territories probably should be a hash set and not a list if (employee.IsValid()) { this.employeeRepository.DbContext.BeginTransaction(); this.employeeRepository.SaveOrUpdate(employee); this.employeeRepository.DbContext.CommitChanges(); } else { this.employeeRepository.DbContext.RollbackTransaction(); } }