示例#1
0
        //public IActionResult Index()
        //{
        //    var gameModels = _games.GetAll();

        //    var listingResults = gameModels
        //        .Select(result => new Game
        //        {
        //            Id = result.Id,
        //            ImageUrl = result.ImageUrl,
        //            Title = result.Title,
        //            Genre = result.Genre,
        //            ReleaseYear = result.ReleaseYear,
        //            Rating = result.Rating,
        //            Platform = result.Platform

        //        });

        //    var model = new GameIndex()
        //    {
        //        Games = listingResults
        //    };

        //    return View(model);
        //}


        public IActionResult Index(string sortOption)
        {
            var gameModels = _games.GetAll();

            var listingResults = gameModels
                                 .Select(result => new Game
            {
                Id          = result.Id,
                ImageUrl    = result.ImageUrl,
                Title       = result.Title,
                Genre       = result.Genre,
                ReleaseYear = result.ReleaseYear,
                Rating      = result.Rating,
                Platform    = result.Platform
            });

            var modelGames = listingResults.ToList();
            var model      = new GameIndex
            {
                Games = modelGames
            };

            switch (sortOption)
            {
            case "Title":
                model.Games = modelGames.OrderBy(game => game.Title);
                break;

            case "Genre":
                model.Games = modelGames.OrderBy(game => game.Genre);
                break;

            case "Platform":
                model.Games = modelGames.OrderBy(game => game.Rating);
                break;

            case "ReleaseYear":
                model.Games = modelGames.OrderByDescending(game => game.ReleaseYear);
                break;

            case "Rating":
                model.Games = modelGames.OrderByDescending(game => game.Rating);
                break;

            default:
                model.Games = modelGames;
                break;
            }

            return(View(model));
        }