public void EditDirector(DirectorEdit newItem) { Director director = dc.Directors.Include("Movies").FirstOrDefault(m => m.Id == newItem.Id); director.Name = newItem.Name; try { if (newItem.movieRemoveId.Count != 0) { foreach (var item in newItem.movieRemoveId) { director.Movies.Remove(dc.Movies.Find(item)); //Addition to remove director from movie dc.Movies.Find(item).Director = null; dc.SaveChanges(); } } } catch (System.NullReferenceException) { } try { if (newItem.MovieId.Count != 0) { foreach (var item in newItem.MovieId) { director.Movies.Add(dc.Movies.Find(item)); dc.Movies.Find(item).Director = director; dc.SaveChanges(); } } } catch (System.NullReferenceException) { } dc.Entry(director).State = EntityState.Modified; dc.SaveChanges(); }
public ActionResult Edit(DirectorEdit newItem) { dir.EditDirector(newItem); return View("Index", dir.GetDirectorsForList()); }