public ViewResult New() { var genres = _context.Genres.ToList(); var viewModel = new ViewModels.NewMoviesViewModel { Genre = genres }; return(View("MovieForm", viewModel)); }
public ActionResult EditMovie(int id) { var movies = _context.Movies.SingleOrDefault(c => c.Id == id); if (movies == null) { return(HttpNotFound()); } var viewModel = new ViewModels.NewMoviesViewModel(movies) { Genre = _context.Genres.ToList() }; return(View("MoviesForm", movies)); }
public ActionResult CreateMovie(Movie movie) { //驗證失敗 if (!ModelState.IsValid) { var viewModel = new ViewModels.NewMoviesViewModel(movie) { Genre = _context.Genres.ToList() }; return(View("MovieForm", viewModel)); } //新增 if (movie.Id == 0) { movie.DateAdded = DateTime.Now; _context.Movies.Add(movie); } //修改 else { var movieInDb = _context.Movies.Single(m => m.Id == movie.Id); movieInDb.Name = movie.Name; movieInDb.GenreId = movie.GenreId; movieInDb.NumInStock = movie.NumInStock; movieInDb.ReleaseDate = movie.ReleaseDate; } try { _context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { Console.WriteLine(e); } return(RedirectToAction("Index", "Movies")); }