public ActionResult Edit(int id, MovieGenresDirectorsRatingsFormats movieVM) { try { //Image if (movieVM.File != null) { movieVM.Movie.ImagePath = movieVM.File.FileName; string target = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(movieVM.File.FileName)); if (!System.IO.File.Exists(target)) { //File doesn't already exist, save it movieVM.File.SaveAs(target); ViewBag.Message = "File uploaded successfully!"; } else { //File already exists with this path ViewBag.Message = "File already exists with this name..."; } } //Genres IEnumerable <int> oldGenreIds = new List <int>(); if (Session["genreids"] != null) { oldGenreIds = (IEnumerable <int>)Session["genreids"]; } IEnumerable <int> newGenreIds = new List <int>(); if (movieVM.GenreIds != null) { newGenreIds = movieVM.GenreIds; } //Identify the deletes IEnumerable <int> deletes = oldGenreIds.Except(newGenreIds); //Identify the adds IEnumerable <int> adds = newGenreIds.Except(oldGenreIds); deletes.ToList().ForEach(d => MovieGenre.Delete(id, d)); adds.ToList().ForEach(a => MovieGenre.Add(id, a)); movieVM.Movie.Update(); return(RedirectToAction("Index")); } catch { return(View(movieVM)); } }
public ActionResult Create(MovieGenresDirectorsRatingsFormats movieVM) { try { //Image if (movieVM.File != null) { movieVM.Movie.ImagePath = movieVM.File.FileName; string target = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(movieVM.File.FileName)); if (!System.IO.File.Exists(target)) { //File doesn't already exist, save it movieVM.File.SaveAs(target); ViewBag.Message = "File uploaded successfully!"; } else { //File already exists with this path ViewBag.Message = "File already exists with this name..."; } } //Movie insert movieVM.Movie.Insert(); //Genre inserts IEnumerable <int> newGenreIds = new List <int>(); if (movieVM.GenreIds != null) { newGenreIds = movieVM.GenreIds; } newGenreIds.ToList().ForEach(a => MovieGenre.Add(movieVM.Movie.Id, a)); return(RedirectToAction("Index")); } catch { return(View(movieVM)); } }