# CoreGridFSP Helper Tags for use with the standard dotnet core 3.1+ MVC razor grid. Adds Filtering and Sorting to grid headers and makes the standard bootstrap pagination work with the grid filters and sorting. ## Common Requirements For any page that will use these `TagHelpers`, some common code needs to be added to the View html: ### Register `CoreGridFSP` for use in the View This will make the `CoreGridFSP` `TagHelpers` available to the View. ``` html @using CoreGridFSP @addTagHelper *, CoreGridFSP ``` ### Setup Required Objects in the Controller Like any other Index controller, you will have to add a parameter for the pagination, sort and any filter you are implementing ```csharp // GET: Movies public async Task Index() { ``` Read all the parameters from the function signature into a new `CoreGridFSPOptions` object ```csharp var gridOptions = this.HttpContext.Request.ExtractCoreGridOptions(); ``` #### Use the FilterList to build out EF `where` clauses ```csharp if (gridOptions.FilterList.ContainsKey("title") && !string.IsNullOrWhiteSpace(gridOptions.FilterList["title"])) movies = movies.Where(m => m.Title.Contains(gridOptions.FilterList["title"])); int genreId; if (gridOptions.FilterList.ContainsKey("GenreId") && int.TryParse(gridOptions.FilterList["GenreId"], out genreId)) movies = movies.Where(m => (m.GenreId == genreId)); ``` #### After filtering the source rows, you can collect some pagination details: ```csharp gridOptions.PaginationOptions.Count = movies.Count(); gridOptions.PaginationOptions.CurrentPage = (!currentPage.HasValue || currentPage.Value == 0) ? 1 : currentPage.Value; ``` For simple sorting, where the sortable-headers are all pointing to a model property, use the supplied QueryExtensions: ```csharp using CoreGridFSP.Extensions; ``` ```csharp if (!String.IsNullOrWhiteSpace(gridOptions.SelectedSortName)) movies = movies.OrderBy(gridOptions.SelectedSortName, gridOptions.SelectedSortDirection == SortableHeaderTagHelper.SortDirection.Desc); ``` Or use the SelectedSort parameters to build out the EF `OrderBy` if you need custom sorting. ```csharp switch(gridOptions.SelectedSort == null?"":gridOptions.SelectedSortName.ToUpper()) { case "TITLE": if(gridOptions.SelectedSortDirection== SortableHeaderTagHelper.SortDirection.Asc) movies = movies.OrderBy(m => m.Title); else movies = movies.OrderByDescending(m => m.Title); break; case "RELEASEDATE": if(gridOptions.SelectedSortDirection == SortableHeaderTagHelper.SortDirection.Asc) movies = movies.OrderBy(m => m.ReleaseDate); else movies = movies.OrderByDescending(m => m.ReleaseDate); break; case "GENREID": if(gridOptions.SelectedSortDirection== SortableHeaderTagHelper.SortDirection.Asc) movies = movies.OrderBy(m => m.Genre.GenreName); else movies = movies.OrderByDescending(m => m.Genre.GenreName); break; case "PRICE": if(gridOptions.SelectedSortDirection== SortableHeaderTagHelper.SortDirection.Asc) movies = movies.OrderBy(m => m.Price); else movies = movies.OrderByDescending(m => m.Price); break; } ``` Add the CoreGridOptions to your view model and return that to the view | Tag Helper | Description | |:--|:--| | [FilterHeaderTagHelper](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/FilterHeader.md) | Adds a Filter Dropdown to a table header for a specific column. | | [PaginationTagHelper](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/Pagination.md) | Adds pagination to the bottom of a table.| | [RowAction](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/RowAction.md) |Makes adding an Action dropdown to a grid simpler.| | [SortableHeaderTagHelper](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/SortableHeader.md) | Adds sort options to a table header| | Option Model | | :--| | [PaginagionOptions](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/models/PaginationOptions.md)| | [CoreGridFSPOptions](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/models/CoreGridFSPOptions.md)| # Extensions A few extensions are available to make controller-side coding simpler. To use include the CoreGridFSP.Extensions namespace: ```csharp using CoreGridFSP.Extensions; ``` | Extension | Desctiption | | :-- | :-- | | InheritedModelExtensions | Copies all common properties from one model to another. Useful when creating view models that inherit from data models. | | QueryExtensions | Allows string column names to be used to sort a data set. | | ExtractCoreGridOptionsExtensions | Automatically generates a CoreGridFSPOptions object from the HttpContext of an GET Index() controller.| # Examples All examples are based on the [MVCMovies tutorial](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-3.1&tabs=visual-studio) provided by Microsoft. - [Index Get method](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/examples/MoviesController.md) - [Index View Model](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/examples/IndexViewModel.md) - [Index View](https://github.com/bmittenzwey/CoreGridFSP/blob/master/CoreGridFSP/documentation/examples/IndexView.md) # References - [CoreGridFSP on NuGet](https://www.nuget.org/packages/CoreGridFSP/) - [CoreGridFSP Source on GitHub](https://github.com/bmittenzwey/CoreGridFSP) - [Get Started with ASP.NET Core MVC](https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-3.1&tabs=visual-studio)