public IActionResult Edit(string id) { Employee employee = this.employeesService.GetEmployeeById(id); if (employee == null) { return(this.View("NotFound")); } Company company = this.companiesService.GetCompanyById(employee.Company.Id); var offices = company.Offices.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = $"{x.City}" }).ToList(); EditEmployeeInputModel model = new EditEmployeeInputModel { Id = employee.Id, FirstName = employee.FirstName, LastName = employee.LastName, StartingDate = employee.StartingDate, Salary = employee.Salary, VacationDays = employee.VacationDays, ExperienceLevel = employee.ExperienceLevel, Offices = offices }; return(this.View(model)); }
public IActionResult Edit(EditEmployeeInputModel model) { Employee employee = this.employeesService.GetEmployeeById(model.Id); Company company = this.companiesService.GetCompanyById(employee.Company.Id); model.Offices = company.Offices.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = $"{x.City}" }).ToList(); if (!this.ModelState.IsValid) { return(this.View(model)); } employee.FirstName = model.FirstName; employee.LastName = model.LastName; employee.StartingDate = model.StartingDate; employee.Salary = model.Salary; employee.VacationDays = model.VacationDays; employee.ExperienceLevel = model.ExperienceLevel; if (model.SelectedOffice != null) { if (employee.Office != null) { Office currentOffice = this.officesService.GetOfficeById(employee.Office.Id); currentOffice.Employees.Remove(employee); this.officesService.UpdateOffice(currentOffice); } Office office = this.officesService.GetOfficeById(model.SelectedOffice); office.Employees.Add(employee); this.officesService.UpdateOffice(office); } this.employeesService.UpdateEmployee(employee); return(this.RedirectToAction("Details", new { @id = model.Id })); }
public async Task <IActionResult> Edit(string id, EditEmployeeInputModel editEmployeeInputModel) { await this.employeeService.EditEmployee(id, editEmployeeInputModel); return(Redirect($"/Company/All")); }