In my previous post, I discussedHow to customize columns in MVC grid view. In this post, I will talk about paging. When you are displaying data in a grid, you probably have lot of rows of data. Some time data set is too large that it is not practical to display all rows on a single page. I will not go into details about why we all should be using paging when displaying large data sets.
WebGrid extension in MVC provides paging capability out of the box. In this post I will keep discussion limited to what is available by default.
@model WebGridSamples.Models.PostCollection @{ var grid = new WebGrid(source: Model.Posts, defaultSort: "DateCreated", rowsPerPage: 25, // Default value is 10 canSort: true, canPage: true, pageFieldName: "pageIndex"); }
If you look at above code snippet, you will notice that there are 3 parameters for WebGrid object that you can use to manipulate paging behavior when creating your grid.
Now that you have set the parameters for paging, next important part of customization is specifying style for your pager. There are two components of styling. One is how the page numbers are going to be rendered and second how do they look.
> @grid.GetHtml( columns: grid.Columns( grid.Column("Title", "Blog title"), grid.Column("Author", "Blog Author"), grid.Column("DateCreated", "Created On", @<i>@item.DateCreated.ToString("yyyy-MM")</i>, "mystyle", false) ), mode: WebGridPagerModes.Numeric| WebGridPagerModes.NextPrevious, tableStyle: "table table-striped", footerStyle: "pagination" )
In above code snippet, there are two properties that allow you to control behavior of pager.
Following code snipper shows how you can get value of selected page in server side in controller implementation.
public ActionResult Index() { int page = 1; page = Int32.TryParse(Request["pageIndex"], out page) ? page : 1; System.Diagnostics.Debug.WriteLine(string.Format("Page: {0}", page)); var blogRepository = new BlogRepository(ConfigurationManager.ConnectionStrings["blogdb"].ConnectionString); var posts = blogRepository.GetAllPosts(); var postCollection = new Models.PostCollection() { PageSize = PageSize, TotalRecords = posts.Count(), Posts = posts.Skip((page - 1)*PageSize).Take(PageSize) }; return View(postCollection); }
As you can see how easy it is to use paging with your MVC grid view. In subsequent posts I will discuss some smart paging options.
MVC Grid - How to customize columns in MVC grid view
How to plan CCSP Exam preparation
Develop a MongoDB pipeline to transform data into time buckets
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2025 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use