// GET: Movies public async Task <IActionResult> Index(string movieGenre, string searchString) { IQueryable <string> genreQuery = from m in _context.Movie orderby m.Genre select m.Genre; var movies = from m in _context.Movie select m; if (!string.IsNullOrEmpty(searchString)) { movies = movies.Where(x => x.Name.Contains(searchString)); } if (!string.IsNullOrWhiteSpace(movieGenre)) { movies = movies.Where(x => x.Genre == movieGenre); } var movieGenreVM = new MoviesGenreViewModel(); movieGenreVM.genres = new SelectList(await genreQuery.Distinct().ToListAsync()); movieGenreVM.movies = await movies.ToListAsync(); return(View(movieGenreVM)); }
public ActionResult Create(Movie movie) { if (!ModelState.IsValid) { var viewModel = new MoviesGenreViewModel { Movie = movie, Genres = _context.Genres.ToList() }; return(View("New", viewModel)); } if (movie.Id == 0) { _context.Movies.Add(movie); } else { var updatedMovie = _context.Movies.Single(m => m.Id == movie.Id); updatedMovie.Name = movie.Name; updatedMovie.NumberInStock = movie.NumberInStock; updatedMovie.GenreId = movie.GenreId; updatedMovie.ReleaseDate = movie.ReleaseDate; } _context.SaveChanges(); return(RedirectToAction("AllMovies", "Movies")); }
public ActionResult New() { var AllGenres = _context.Genres.ToList(); var moviesGenreViewModel = new MoviesGenreViewModel { Genres = AllGenres }; return(View(moviesGenreViewModel)); }
public ActionResult Edit(int id) { var movie = _context.Movies.SingleOrDefault(m => m.Id == id); if (movie == null) { return(HttpNotFound()); } var viewModel = new MoviesGenreViewModel { Movie = movie, Genres = _context.Genres.ToList() }; return(View("New", viewModel)); }