Example #1
0
        // GET: /Movie/
        public ActionResult Index(string movieTitle, string genreTitle, string sortOrder,
                                  string currentTitle, string currentGenre, int?page)
        {
            var movies = _movieService.GetAll();
            var genres = _genreService.GetAll();

            if (movieTitle != null || genreTitle != null)
            {
                page = 1;
            }
            else
            {
                movieTitle = currentTitle;
                genreTitle = currentGenre;
            }
            ViewBag.CurrentTitle = movieTitle;
            ViewBag.genreTitle   = new SelectList(genres, "Title", "Title");
            ViewBag.CurrentGenre = genreTitle;

            var movieViewModel = Mapper.Map <IEnumerable <Movie>, IEnumerable <MovieViewModel> >(movies);

            FilterByTitleOrGenre(movieTitle, genreTitle, ref movieViewModel);

            foreach (var movie in movieViewModel)
            {
                movie.Rating = _ratingService.CountRatingByMovieId(movie.Id);
            }

            ViewBag.CurrentSort   = sortOrder;
            ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "title_desc" : "";
            ViewBag.DateSortParm  = sortOrder == "Date" ? "date_desc" : "Date";
            ViewBag.RateSortParm  = sortOrder == "Rate" ? "rate_desc" : "Rate";
            SortMovies(sortOrder, ref movieViewModel);

            const int pageSize   = 5;
            int       pageNumber = (page ?? 1);

            return(View(movieViewModel.ToPagedList(pageNumber, pageSize)));
        }