//This constructor stores filtering route segments, as well as //the paging and sorting route segments stored by the base constructor public BooksGridBuilder(ISession sess, BookGridDTO values, string defaultSortFilter) : base(sess, values, defaultSortFilter) { //store filter route segments - add filter prefixes if this is inital load //of page with default values rather than route values (route values have prefix) bool isInitial = values.Genre.IndexOf(FilterPrefix.Genre) == -1; Routes.AuthorFilter = (isInitial) ? FilterPrefix.Author + values.Author : values.Author; Routes.GenreFilter = (isInitial) ? FilterPrefix.Genre + values.Genre : values.Genre; Routes.PriceFilter = (isInitial) ? FilterPrefix.Price + values.Price : values.Price; SaveRouteSegment(); }
//DTO has properties for the paging, soreting, and filtering route segments defined in teh Startup.cs file public ViewResult List(BookGridDTO values) { //get grid builder, which loads route segment values and stores them in session var builder = new BooksGridBuilder(HttpContext.Session, values, defaultSortFilter: nameof(Book.Title)); //create a BookQueryOPtions object to build a query expresssion for a page of data var options = new BookQueryOptions { Includes = "BookAuthors.Author, Genre", OrderByDirection = builder.CurrentRoute.SortDirection, PageNumber = builder.CurrentRoute.PageNumber, PageSize = builder.CurrentRoute.PageSize }; //call the SortFilter() method of the BookQueryOPtions object and pass it to the builder //object. It uses the route information and the properties of the builder object to //add sort and filter options to the query expression. options.SortFilter(builder); //create view model and add page of book data, data for drop-downs, //the current route, and the total number of pages. var vm = new BookListViewModel { Books = data.Books.List(options), Authors = data.Authors.List(new QueryOptions <Author> { OrderBy = a => a.FirstName }), Genres = data.Genres.List(new QueryOptions <Genre> { OrderBy = g => g.Name }), CurrentRoute = builder.CurrentRoute, TotalPages = builder.GetTotalPages(data.Books.Count) }; return(View(vm)); }