public IActionResult Create(string id) { Company company = this.companiesService.GetCompanyById(id); var offices = company.Offices.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = $"{x.City}" }).ToList(); CreateEmployeeInputModel model = new CreateEmployeeInputModel { Offices = offices }; return(this.View(model)); }
public IActionResult Create(CreateEmployeeInputModel model) { Company company = this.companiesService.GetCompanyById(model.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 employee = new Employee { FirstName = model.FirstName, LastName = model.LastName, StartingDate = model.StartingDate, Salary = model.Salary, VacationDays = model.VacationDays, ExperienceLevel = model.ExperienceLevel }; if (model.SelectedOffice != null) { string officeId = model.SelectedOffice; Office office = this.officesService.GetOfficeById(officeId); office.Employees.Add(employee); } company.Employees.Add(employee); this.companiesService.UpdateCompany(company); return(this.RedirectToAction("Details", "Companies", new { @id = model.Id })); }