public ViewResult List(BooksGridDTO values) { var builder = new BooksGridBuilder(HttpContext.Session, values, defaultSortField: nameof(Book.Title)); var options = new BookQueryOptions { Include = "BookAuthors.Author, Genre", OrderByDirection = builder.CurrentRoute.SortDirection, PageNumber = builder.CurrentRoute.PageNumber, PageSize = builder.CurrentRoute.PageSize }; options.SortFilter(builder); 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)); }
// dto has properties for the paging, sorting, and filtering route segments defined in the Startup.cs file public ViewResult List(BooksGridDTO values) { // get grid builder, which loads route segment values and stores them in session var builder = new BooksGridBuilder(HttpContext.Session, values, defaultSortField: nameof(Book.Title)); // create a BookQueryOptions object to build a query expression 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 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) }; // pass view model to view return(View(vm)); }