public ActionResult SignPlayers(List <PlayersStartSquad> model, FormCollection collection) { PlayerProcessor playerProcessor = new PlayerProcessor(); MatchProcessor matchProcessor = new MatchProcessor(); Organization org = Session["MyClub"] as Organization; foreach (var item in model) { Selection selection = new Selection(); var player = matchProcessor.RetrievePlayerForMatch(model.First().matchId, org.Id, item.playerId); if (item.isFirstSelection || item.isSubstitution) { if (player != null) { if (item.isFirstSelection) { selection = playerProcessor.RetrieveSelection(1); } else { selection = playerProcessor.RetrieveSelection(2); } player.Selection = selection; if (item.isFirstSelection && item.isCaptain) { player.Captain = 1; } matchProcessor.UpdateMatchPerson(player); } else { MatchPerson matchPerson = new MatchPerson(); matchPerson.Match = new Match(); matchPerson.Organization = new Organization(); matchPerson.Person = new Person(); matchPerson.Selection = new Selection(); if (item.isFirstSelection && item.isCaptain) { matchPerson.Captain = 1; } Person person = playerProcessor.RetrievePlayer(item.playerId); Match match = matchProcessor.RetrieveMatch(item.matchId); if (item.isFirstSelection) { selection = playerProcessor.RetrieveSelection(1); } else { selection = playerProcessor.RetrieveSelection(2); } matchPerson.Person = person; matchPerson.Match = match; matchPerson.Selection = selection; matchPerson.Organization = org; matchProcessor.UpdateMatchPerson(matchPerson); } } else { if (player != null) { matchProcessor.DeleteMatchPerson(player); } } } return(RedirectToAction("Index")); }