public IActionResult UpdateAct(Guid id) { var act = _context.Acts .Where(a => a.Id == id) .Include(a => a.Events) .ThenInclude(ea => ea.Event) .Single(); var eventsFromAct = act.Events.Select(ea => ea.Event); var model = new ActsUpdateActVM() { Id = act.Id, Name = act.Name, Events = _context.Events.Select(e => new ActsEventActVM() { Name = e.Name, Id = e.Id, IsChecked = eventsFromAct.Contains(e) }).ToList() }; ViewBag.Title = "Update act"; return(View(model)); }
public IActionResult UpdateAct(ActsUpdateActVM model) { var act = _context.Acts .Where(a => a.Id == model.Id) .Include(a => a.Events) .ThenInclude(ea => ea.Event) .Single(); act.Name = model.Name; if (model.Events != null) { foreach (var ev in model.Events) { var e = _context.EventActs.SingleOrDefault(ea => ea.ActId == act.Id && ea.EventId == ev.Id); if (ev.IsChecked) { if (e == null) { _context.EventActs.Add(new EventAct() { Act = act, Event = _context.Events.FirstOrDefault(evt => evt.Id == ev.Id) }); } } else { if (e != null) { _context.EventActs.Remove(e); } } } } _context.SaveChanges(); TempData["ActUpdated"] = $"Act {act.Name} has been updated."; ViewBag.Title = "Update act"; return(RedirectToAction("Overview")); }