简体   繁体   中英

How does MVC WebGrid update the query string

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:

    public string SortColumn {
        get { 
            if (!_sortColumnSet) { 
                string sortColumn = QueryString[SortFieldName];
                // blah blah blah
            } 
            // blah blah blah
            return _sortColumn;
        } 
        set {
            // blah blah blah omitted for brevity
        } 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM