public ActionResult PickGames(BulkPickEditor bpe) { bpe.Player = db.Players.Find(bpe.PlayerId); var pgs = from g in db.Games.Where(q => q.Week == bpe.Week && q.Year == bpe.Year) join p in db.Picks.Where(q => q.PlayerId == bpe.PlayerId) on g.Id equals p.GameId into j1 from j2 in j1.DefaultIfEmpty() select new { Game = g, Pick = j2 }; List<Pick> picks = new List<Pick>(); foreach (var pg in pgs) { Pick pick = pg.Pick ?? new Pick(); pick.GameId = pg.Game.Id; pick.Game = pg.Game; pick.PlayerId = bpe.PlayerId; pick.Player = bpe.Player; picks.Add(pick); } bpe.Picks = picks; return View(bpe); }
public ActionResult SubmitPicks(BulkPickEditor bpe) { if (ModelState.IsValid) { foreach (var pick in bpe.Picks) { if (pick.Id != 0) { db.Entry(pick).State = System.Data.Entity.EntityState.Modified; } else { db.Picks.Add(pick); } } db.SaveChanges(); } return RedirectToAction("Index"); }