简体   繁体   English

MVC WebGrid如何更新查询字符串

[英]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. 在MVC的WebGrid上启用分页和排序时,它会自动在查询字符串中附加排序和页面参数。 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? 我理解它是如何为页面n创建链接的,但如果读取查询字符串以了解要生成哪个页面怎么办?

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 这是gridview定义

@{ 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 和我的Customer.Index控制器

//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. 简而言之,它只是从HttpContext.Request.QueryString获取值,这不是那种神奇的事情。

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
        } 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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