public async Task <IActionResult> Create([Bind("ID,Name,Desc,EstCost,BidDate,EstStartDate,EstFinishDate,Cost,BidCustApproved,BidManagementApproved,ClientID,DesignerID")] Project project, string[] selectedLabOptions, string[] selectedLabQuantity, string[] selectedMatOptions, string[] selectedMatQuantity) { try { int ctr = 0; int a = 0; //Add the selected conditions if (selectedLabOptions != null) { project.LabourSummaries = new List <LabourSummary>(); foreach (var employeeType in selectedLabOptions) { var employeeTypeToAdd = new LabourSummary { ProjectID = project.ID, EmployeeTypeID = int.Parse(employeeType), Hours = int.Parse(selectedLabQuantity[ctr]) }; project.LabourSummaries.Add(employeeTypeToAdd); ctr++; } } if (selectedMatOptions != null) { project.ProjectMaterials = new List <ProjectMaterials>(); foreach (var inventory in selectedMatOptions) { var inventoryToAdd = new ProjectMaterials { ProjectID = project.ID, InventoryID = int.Parse(inventory), MatEstQty = int.Parse(selectedMatQuantity[a]) }; project.ProjectMaterials.Add(inventoryToAdd); a++; } } if (ModelState.IsValid) { _context.Add(project); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (RetryLimitExceededException /* dex */) { ModelState.AddModelError("", "Unable to save changes after multiple attempts. Try again, and if the problem persists, see your system administrator."); } catch (Exception) { ModelState.AddModelError("", "Unknown error!"); } PopulateAssignedEmployeeTypes(project); PopulateAssignedMaterials(project); PopulateDropDownLists(project); return(View(project)); }
private void UpdateEmployeeTypes(string[] selectedOptions, string[] selectedQuantity, Project projectToUpdate) { if (selectedOptions == null) { projectToUpdate.LabourSummaries = new List <LabourSummary>(); return; } int ctr = 0; var selectedEmployeeTypeHS = new HashSet <string>(selectedOptions); var selectedEmployeeTypeHoursHS = new HashSet <string>(selectedQuantity); var LabourSummariesHS = new HashSet <int> (projectToUpdate.LabourSummaries.Select(c => c.EmployeeTypeID)); foreach (var employeeType in _context.EmployeeTypes) { if (selectedEmployeeTypeHS.Contains(employeeType.ID.ToString())) { if (LabourSummariesHS.Contains(employeeType.ID)) { LabourSummary employeeTypeToRemove = projectToUpdate.LabourSummaries.SingleOrDefault(c => c.EmployeeTypeID == employeeType.ID); _context.Remove(employeeTypeToRemove); projectToUpdate.LabourSummaries.Add(new LabourSummary { ProjectID = projectToUpdate.ID, EmployeeTypeID = employeeType.ID, Hours = int.Parse(selectedQuantity[ctr]) }); } if (!LabourSummariesHS.Contains(employeeType.ID)) { projectToUpdate.LabourSummaries.Add(new LabourSummary { ProjectID = projectToUpdate.ID, EmployeeTypeID = employeeType.ID, Hours = int.Parse(selectedQuantity[ctr]) }); } ctr++; } else { if (LabourSummariesHS.Contains(employeeType.ID)) { LabourSummary employeeTypeToRemove = projectToUpdate.LabourSummaries.SingleOrDefault(c => c.EmployeeTypeID == employeeType.ID); _context.Remove(employeeTypeToRemove); } } } }