public ActionResult Save(Movie movie) { if (!ModelState.IsValid) { var movieViewModel = new MovieForViewModel() { Movie = movie, Genre = _context.Genre.ToList() }; return(View("MovieForm", movieViewModel)); } if (movie.Id == 0) { movie.DateAdded = DateTime.Now; _context.Movies.Add(movie); } else { var movieInDb = _context.Movies.Single(s => s.Id == movie.Id); movieInDb.GenreId = movie.GenreId; movieInDb.Name = movie.Name; movieInDb.NumberInStock = movie.NumberInStock; movieInDb.ReleaseDate = movie.ReleaseDate; } _context.SaveChanges(); return(RedirectToAction("Index", "Movies")); }
public ActionResult New() { var movie = new MovieForViewModel { Movie = new Movie(), Genres = _context.Genres.ToList() }; return(View("MovieForm", movie)); }
public ActionResult New() { var genres = _context.Genre.ToList(); MovieForViewModel movieViewModel = new MovieForViewModel() { Genre = genres }; return(View("MovieForm", movieViewModel)); }
public ActionResult Edit(int id) { var movie = _context.Movies.FirstOrDefault(m => m.Id == id); if (movie == null) { HttpNotFound(); } var viewModel = new MovieForViewModel { Movie = movie, Genres = _context.Genres.ToList() }; return(View("MovieForm", viewModel)); }
public ActionResult Edit(int?id) { var editMovie = _context.Movies.SingleOrDefault(s => s.Id == id); if (editMovie == null) { return(HttpNotFound()); } else { MovieForViewModel movieViewModel = new MovieForViewModel() { Genre = _context.Genre, Movie = editMovie }; return(View("MovieForm", movieViewModel)); } }
public ActionResult Save(Movie movie) { if (!ModelState.IsValid) { var movieView = new MovieForViewModel { Movie = movie, Genres = _context.Genres.ToList() }; return(View("MovieForm", movieView)); } if (movie.Id == 0) { _context.Movies.Add(movie); } else { var dbMovie = _context.Movies.Single(m => m.Id == movie.Id); dbMovie.Name = movie.Name; dbMovie.DateAdded = movie.DateAdded; dbMovie.ReleaseDate = movie.ReleaseDate; dbMovie.NumberInStock = movie.NumberInStock; dbMovie.GenreId = movie.GenreId; } try { _context.SaveChanges(); } catch (DbEntityValidationException ex) { Console.WriteLine(ex.EntityValidationErrors); } return(RedirectToAction("Index", "Movies")); }