public ActionResult Edit(int?id, TeamEdit newItem)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Where(x => x.Value.Errors.Any())
                             .Select(x => new { x.Key, x.Value.Errors });
                return(RedirectToAction("edit", new { id = newItem.TeamId }));
            }

            if (id.GetValueOrDefault() != newItem.TeamId)
            {
                return(RedirectToAction("index"));
            }

            var editedItem = m.TeamEdit(newItem);

            if (editedItem == null)
            {
                return(RedirectToAction("edit", new { id = newItem.TeamId }));
            }
            else
            {
                return(RedirectToAction("DetailsWithMediaItems", new { id = newItem.TeamId }));
            }
        }
Beispiel #2
0
        //Team edit
        public TeamWithDetail TeamEdit(TeamEdit newItem)
        {
            //get associated item
            var o = ds.Teams.Include("Players").Include("Coaches").SingleOrDefault(t => t.TeamID == newItem.TeamId);

            if (o == null)
            {
                return(null);
            }
            else
            {
                //clear players
                o.Players.Clear();

                foreach (var item in newItem.PlayerIds)
                {
                    var add = ds.Players.Find(item);
                    add.TeamName = o.TeamName;
                    o.Players.Add(add);
                }

                ds.SaveChanges();
            }

            //now save changes made to team that doesnt deal with players
            var obj = ds.Teams.Find(newItem.TeamId);

            if (obj == null)
            {
                return(null);
            }
            else
            {
                ds.Entry(obj).CurrentValues.SetValues(newItem);
                ds.SaveChanges();

                return(Mapper.Map <TeamWithDetail>(o));
            }
        }