public async Task <IActionResult> Edit(int id, Session session) { if (id != session.Id) { return(NotFound()); } if (ModelState.IsValid) { try { foreach (PlayerForSession ps in session.PlayerForSessions) { ps.SessionId = session.Id; if (_context.PlayerForSessions.Any(x => x.SessionId == ps.SessionId && x.PlayerId == ps.PlayerId) && ps.isChecked == false) { session.PlayerForSessions.Remove(ps); _context.RemoveRange(_context.PlayerForSessions.Where(x => x.SessionId == ps.SessionId && x.PlayerId == ps.PlayerId)); } if (_context.PlayerForSessions.Any(x => x.SessionId == ps.SessionId && x.PlayerId == ps.PlayerId) && ps.isChecked == true) { session.PlayerForSessions.Remove(ps); } if (ps.isChecked == false) { session.PlayerForSessions.Remove(ps); } if (ps.isChecked == true) { ps.Attended = true; if (ps.Notes == null) { ps.Notes = ""; } } } _context.Update(session); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SessionExists(session.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["GroupId"] = new SelectList(_context.Groups, "Id", "GroupName", session.GroupId); ViewData["PostId"] = new SelectList(_context.Posts, "Id", "PostHeader", session.PostId); ViewData["TermId"] = new SelectList(_context.Terms, "Id", "TermName", session.TermId); foreach (Player p in _context.Players) { if (session.PlayerForSessions.Any(x => x.PlayerId == p.Id)) { session.PlayerForSessions.Where(x => x.PlayerId == p.Id).First().isChecked = true; } else { PlayerForSession ps = new PlayerForSession() { PlayerId = p.Id, Player = p }; session.PlayerForSessions.Add(ps); } } session.PlayerForSessions.OrderBy(x => x.Player.LastName); return(View(session)); }
public async Task <IActionResult> Edit(int id, Post post) { if (id != post.Id) { return(NotFound()); } if (ModelState.IsValid) { try { foreach (BlogPostRec pr in post.BlogPostRecs) { pr.PostId = post.Id; if (_context.BlogPostRecs.Any(x => x.PostId == pr.PostId && x.BlogId == pr.BlogId) && pr.isChecked == false) { post.BlogPostRecs.Remove(pr); _context.RemoveRange(_context.BlogPostRecs.Where(x => x.PostId == pr.PostId && x.BlogId == pr.BlogId)); } if (_context.BlogPostRecs.Any(x => x.PostId == pr.PostId && x.BlogId == pr.BlogId) && pr.isChecked == true) { post.BlogPostRecs.Remove(pr); } if (pr.isChecked == false) { post.BlogPostRecs.Remove(pr); } } _context.Update(post); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(post.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } foreach (Blog b in _context.Blogs) { if (post.BlogPostRecs.Any(x => x.BlogId == b.Id)) { post.BlogPostRecs.Where(x => x.BlogId == b.Id).First().isChecked = true; } else { BlogPostRec bpr = new BlogPostRec() { BlogId = b.Id, Blog = b }; post.BlogPostRecs.Add(bpr); } } post.BlogPostRecs.OrderBy(x => x.Blog.BlogName); return(View(post)); }