Esempio n. 1
0
        public async Task <ActionResult> Index(string q, string genre, int?page)
        {
            try
            {
                var movies = _movie.GetAllMoviesAsQueryable();

                // filter with query
                if (!String.IsNullOrEmpty(q))
                {
                    ViewBag.currentQuery = q;
                    movies = movies.Where(m => m.Title.Contains(q));
                }

                // filter with genre
                if (!String.IsNullOrEmpty(genre))
                {
                    if (!Enum.IsDefined(typeof(Genres), genre))
                    {
                        return(RedirectToAction("index", "movies"));
                    }

                    ViewBag.currentGenre = genre;
                    int genreValue = (int)Enum.Parse(typeof(Genres), genre);
                    movies = movies.Where(m => m.MovieGenres.Any(mg => mg.Genre == genreValue));
                }


                ViewBag.Genres = new SelectList(Enum.GetNames(typeof(Genres)));
                return(View(await GetPaginatedMovies(_pagination.GetMoviesPagination(page, 5), movies)));
            }
            catch (Exception e)
            {
                _logger.Writer.Fatal(e, "/movies View could not be loaded.");
                return(View("Error"));
            }
        }