using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Domain.Repositories; using FilmsRUsOnline.Models; namespace FilmsRUsOnline.Controllers { public class NavigationController : Controller { private IFilmsRepository filmsRepository; public NavigationController(IFilmsRepository filmsRepository) { this.filmsRepository = filmsRepository; } // First implementation of DisplayMenu(), to get the ball rolling. /* public string DisplayMenu() { return "Hi from Nav"; } */ // Enhanced version of DisplayMenu(), to actually display a menu :-) public ViewResult DisplayMenu(string genre) { Func makeLink = genreName => new NavigationLink { Text = genreName ?? "Home", RouteValues = new RouteValueDictionary(new { controller = "Films", action = "List", genre = genreName, pageNumber = 1 }), IsSelected = (genreName == genre) }; // Put a Home link at the top. List links = new List(); links.Add(makeLink(null)); // Add a link for each distinct genre. var genres = filmsRepository.Films.Select(x => x.Genre); foreach (string genreName in genres.Distinct().OrderBy(x => x)) links.Add(makeLink(genreName)); // Return the information to the default view. return View(links); } } }