public ActionResult Edit(MovieEdit mov)
        {
            man.EditMovie(mov);

            //return RedirectToAction("Index");

            return RedirectToAction("Details", new { Id = mov.Id });
        }
Ejemplo n.º 2
0
        public void EditMovie(MovieEdit newItem)
        {
            var itemToEdit = dc.Movies.Include("Genres").Include("Director").FirstOrDefault( m=> m.Id==newItem.Id);

            itemToEdit.Id = newItem.Id;

            itemToEdit.MovieTitle = newItem.MovieTitle;

            itemToEdit.TicketPrice = newItem.TicketPrice;

            //Director dirPrevious = dc.Directors.Find(newItem.Id);

            //itemToEdit.Director = dc.Directors.Find(newItem.DirectorId);

            //if (!(dirPrevious.Id == newItem.DirectorId))
            //    dc.Directors.Find(newItem.DirectorId).Movies.Add(itemToEdit);
            //else
            //{
            //    dc.Directors.Find(newItem.DirectorId).Movies.Remove(itemToEdit);
            //}

            if (!(dc.Directors.Find(newItem.DirectorId).Movies.Contains(itemToEdit)))
                dc.Directors.Find(newItem.DirectorId).Movies.Add(itemToEdit);
            else
            {
                dc.Directors.Find(newItem.DirectorId).Movies.Remove(itemToEdit);
            }

            try
            {

                if (newItem.genreRemoveId.ToList().Count != 0)
                {
                    foreach (var item in newItem.genreRemoveId.ToList())
                    {

                        itemToEdit.Genres.Remove(dc.Genres.Find(item));
                        dc.Genres.Find(item).Movies.Remove(dc.Movies.Find(itemToEdit.Id));
                        dc.SaveChanges();
                    }
                }
            }
            catch (System.ArgumentNullException)
            {

            }

            try
            {
                if (newItem.GenreId.ToList().Count != 0)
                {

                    foreach (var item in newItem.GenreId.ToList())
                    {

                        itemToEdit.Genres.Add(dc.Genres.Find(item));
                        dc.Genres.Find(item).Movies.Add(dc.Movies.Find(newItem.Id));
                        dc.SaveChanges();

                    }

                }
            }

            catch (System.ArgumentNullException)
            {

            }

            //dc.Entry(dc.Movies.Find(newItem.Id)).CurrentValues.SetValues(itemToEdit);
            dc.Entry(itemToEdit).State = EntityState.Modified;
                dc.SaveChanges();
        }