Ejemplo n.º 1
0
        public ActionResult Edit([Bind(Include = "Id, Name")] EditTeamViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Team team = db.Teams.Find(viewModel.Id);
                if (team != null)
                {
                    team.Name = viewModel.Name;
                    try
                    {
                        db.Entry(team).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        return(View("Error", new HandleErrorInfo(ex, "Teams", "Index")));
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    Exception ex = new Exception("Unable to Retrive Team");
                    return(View("Error", new HandleErrorInfo(ex, "Teams", "Index")));
                }
            }

            return(View(viewModel));
        }
        public ActionResult Edit([Bind(Include = "Id, Name, TeamIds")] EditPlayerViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Player player = db.Players.Find(Guid.Parse(viewModel.Id));
                if (player == null)
                {
                    return(HttpNotFound());
                }

                player.Name = viewModel.Name;

                if (viewModel.TeamIds.Count > 0)
                {
                    List <Team> viewModelTeams = new List <Team>();

                    foreach (var id in viewModel.TeamIds)
                    {
                        var team = db.Teams.Find(Guid.Parse(id));

                        if (team != null)
                        {
                            try
                            {
                                player.Teams.Add(team);
                                viewModelTeams.Add(team);
                            }
                            catch (Exception ex)
                            {
                                return(View("Error", new HandleErrorInfo(ex, "Players", "Index")));
                            }
                        }
                    }

                    var allTeams = db.Teams.ToList();

                    var teamsToRemove = allTeams.Except(viewModelTeams);

                    foreach (var team in teamsToRemove)
                    {
                        try
                        {
                            player.Teams.Remove(team);
                        }
                        catch (Exception ex)
                        {
                            return(View("Error", new HandleErrorInfo(ex, "Players", "Index")));
                        }
                    }
                }

                try
                {
                    db.Entry(player).State = EntityState.Modified;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return(View("Error", new HandleErrorInfo(ex, "Players", "Index")));
                }

                return(RedirectToAction("Details", new { id = player.Id }));
            }

            return(View(viewModel));
        }