Search code examples
c#asp.net-mvcasp.net-mvc-3razorwebgrid

Check if any http query exists, not by parameter


Since MVC3 WebGrid sorting default is ascending via query string, &sortdir=ASC.. I would like to know how to sort initially by descending.

I've tried below, using Request.QueryString, but in the case were there is absolutely no query string "?..", this doesn't see, to be working:

// Force a descending sort on page load when query string is empty
if(Request.QueryString[grid.SortDirectionFieldName].IsEmpty()){
    grid.SortDirection = SortDirection.Descending;
}

Since I have a path like ..Admin/Review initially, and not ../Admin/Review?sort=Question6&sortdir=ASC, how can I test this case? Would the above condition still return true if there isn't even a query parameter?

I believe I need to extract a query from the raw url and if it doesn't exist, set my sort direction to descending.


Solution

  • ended up using JS:

    $(document).ready(function () {
        var ignoreURL = window.location.href.replace('DESC', 'ASC');
        $('#grid th a').each(function () {
            if (this.href.indexOf('ASC') > -1 && this.href != ignoreURL) {
                this.href = this.href.replace('ASC', 'DESC');
            }
        });
    });