public async Task <OperationResult> AppointEmployeesToProject(ProjectForDisplay model) { var result = new OperationResult(); try { var projEmployees = context.ProjectEmployees.Where(x => x.ProjectId == model.Id); context.ProjectEmployees.RemoveRange(projEmployees); if (model.Employees != null) { foreach (var empId in model.Employees) { context.ProjectEmployees.Add(new ProjectEmployee { ProjectId = model.Id, EmployeeId = empId }); } } await context.SaveChangesAsync(); result.Succeeded = true; } catch (Exception ex) { result.Message = ex.Message; } return(result); }
public async Task <IActionResult> AppointEmployees(ProjectForDisplay model) { ProjectEmployees projectEmployees; if (!ModelState.IsValid) { projectEmployees = await projectService.GetEmployeesToProjectAsync(model.Id); ViewBag.Employees = projectEmployees.Employees.Select(x => new SelectListItem(x.Value, x.Key.ToString(), projectEmployees.CurrentEmployeesIds.Contains(x.Key))); return(View(model)); } var result = await projectService.AppointEmployeesToProject(model); if (result.Succeeded) { return(RedirectToAction(nameof(Index))); } projectEmployees = await projectService.GetEmployeesToProjectAsync(model.Id); ViewBag.Employees = projectEmployees.Employees.Select(x => new SelectListItem(x.Value, x.Key.ToString(), projectEmployees.CurrentEmployeesIds.Contains(x.Key))); ModelState.AddModelError(string.Empty, result.Message); return(View(model)); }