public TeamWithDetail TeamTrade(TeamTrade newItem) { // find the two teams in the trade var team1 = ds.Teams.Include("Players").SingleOrDefault(t => t.TeamID == newItem.TeamId); var team2 = ds.Teams.Include("Players").SingleOrDefault(t => t.TeamID == newItem.SecondTeamId); // make sure valid teams were selected if (team1 == null || team2 == null) { return(null); } else { foreach (var selectedPlayer in newItem.PlayerIds) { var player = ds.Players.Find(selectedPlayer); // if valid player if (player != null) { // you can d othe same swap you're doing here if (player.TeamName != team1.TeamName) { // player is moving to team 1 team2.Players.Remove(player); team1.Players.Add(player); // update player as well ? player.TeamName = team1.TeamName; player.Teams.Remove(team2); } else { // player is moving to team 12 team1.Players.Remove(player); team2.Players.Add(player); // update player as well ? player.TeamName = team2.TeamName; player.Teams.Remove(team1); } } ds.SaveChanges(); } return(Mapper.Map <TeamWithDetail>(team1)); } }
public ActionResult TeamTrade(TeamTrade newItem) { if (!ModelState.IsValid) { var errors = ModelState.Where(x => x.Value.Errors.Any()) .Select(x => new { x.Key, x.Value.Errors }); return(RedirectToAction("TeamTrade")); } var editedItem = m.TeamTrade(newItem); if (editedItem == null) { return(RedirectToAction("TeamTrade")); } else { return(RedirectToAction("DetailsWithMediaItems", new { id = newItem.TeamId })); } }