public async Task <IActionResult> OnPostAsync(int?id) { _log.LogWarning($"OnPost: ProjectID {id}, ADD members {MemberIdToAdd}"); if (id == null) { return(NotFound()); } Project = await _context.Projects.Include(pm => pm.ProjectMembers).ThenInclude(m => m.Member).FirstOrDefaultAsync(m => m.ProjectID == id); var memberList = _context.Members.Select(p => new { ID = p.MemberID, Display = string.Format($"{p.FirstName} {p.LastName}") }).Select(p => p); MemberDropDown = new SelectList(await memberList.Distinct().ToListAsync(), "ID", "Display"); if (!_context.ProjectMembers.Any(pm => pm.MemberID == MemberIdToAdd && pm.ProjectID == id.Value)) { ProjectMember memberToAdd = new ProjectMember { ProjectID = id.Value, MemberID = MemberIdToAdd }; _context.Add(memberToAdd); _context.SaveChanges(); } else { _log.LogWarning("Project already has this member"); } MemberIdToAdd = 0; return(RedirectToPage(new { id = id })); }
public async Task <IActionResult> OnPostAsync(int?id) { _log.LogWarning($"OnPost: MemberId {id}, ADD project {ProjectIdToAdd}"); if (ProjectIdToAdd == 0) { ModelState.AddModelError("ProjectIdToAdd", "This field is a required field."); return(Page()); } if (id == null) { return(NotFound()); } Member = await _context.Members.Include(pm => pm.ProjectMembers).ThenInclude(m => m.Member).FirstOrDefaultAsync(m => m.MemberID == id); AllProjects = await _context.Projects.ToListAsync(); ProjectDropDown = new SelectList(AllProjects, "ProjectID", "ProjectName"); if (Member == null) { return(NotFound()); } if (!_context.ProjectMembers.Any(pm => pm.ProjectID == ProjectIdToAdd && pm.MemberID == id.Value)) { ProjectMember projectToAdd = new ProjectMember { MemberID = id.Value, ProjectID = ProjectIdToAdd }; _context.Add(projectToAdd); _context.SaveChanges(); } else { _log.LogWarning("Member already in project"); } return(RedirectToPage(new { id = id })); }