When you enable paging and sorting on MVC’s WebGrid, it automatically appends sort and page parameters in the query string automatically. How does it do that? I understand how it creates a link for page n, but how does if read the query string to know what page to produce?
What really confuses me is that, in the controller, I don’t have to specify the page and sort parameters, but they work anyway. What manner of witchcraft is this?
in case I wasn’t clear enough,
here is the gridview definition
@{ var grid = new WebGrid(Model.Customers, rowsPerPage: 25, canPage: true }); }
here is the query string that is produced:
/Customer?sort=Notes&sortdir=ASC
and my Customer.Index controller
//no parameters here. how does WebGrid maintain querystring?
public ActionResult Index()
{
...
}
Long story short, it just plucks the values from
HttpContext.Request.QueryString, which is not that magical afterall.For example, here’s where it accesses the sort field: