public ActionResult Create(Team team, int[] memebersm, int[] eventsm) { if (team != null) { EditListItems(team, memebersm, eventsm); _db.Team.Add(team); _db.SaveChanges(); Success("Your information was saved!"); return RedirectToAction("Index"); } Error("there were some errors in your form."); SetUpViewData(); return View(new Team()); }
public ActionResult Edit(Team team, int[] memebersm, int[] eventsm) { if (team != null) { _db.Entry(team).State = EntityState.Modified; _db.SaveChanges(); EditListItems(team, memebersm, eventsm); _db.SaveChanges(); Success("Your information was saved!"); return RedirectToAction("Index"); } SetUpViewData(); return View("Create", new Team()); }
private void EditListItems(Team team, int[] memebersm, int[] eventsm) { if (memebersm != null && memebersm.Length > 0) { var sportsmans = _db.Sportsmans.Where(x => x.Team.Id == team.Id); foreach (var item in sportsmans) { item.Team = null; } team.Memebers = new List<Sportsman>(); foreach (var item in _db.Sportsmans.Where(x => memebersm.Contains(x.Id))) { team.Memebers.Add(item); } } if (eventsm != null && eventsm.Length > 0) { var ev = _db.Events.Where(x => x.Teams.Any(i => i.Id == team.Id)); foreach (var item in ev) { item.Teams.Remove(team); } team.Events = new List<Event>(); foreach (var item in _db.Events.Where(x => eventsm.Contains(x.Id))) { team.Events.Add(item); } } }