Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar problem inside an HtmlHelper; I wanted to generate links that linked backed to the current page, with a small adjustment in parameters (think incrementing the page number). So if I had URL /Item/?sort=Name&amp;page=0, I wanted to be able to create links to the same page, but just change the page parameter, and have the sort parameter automatically included (ie /Item/?sort=Name&amp;page=1).</p> <p>My solution was this (for use in an HtmlHelper extension method, but since you can access the same data almost anywhere in MVC, you can adapt it easily to your uses):</p> <pre><code>private static RouteValueDictionary CreateRouteToCurrentPage(HtmlHelper html) { RouteValueDictionary routeValues = new RouteValueDictionary(html.ViewContext.RouteData.Values); NameValueCollection queryString = html.ViewContext.HttpContext.Request.QueryString; foreach (string key in queryString.Cast&lt;string&gt;()) { routeValues[key] = queryString[key]; } return routeValues; } </code></pre> <p>What the method does is take the RouteValueDictionary for the current request and create a copy of it. Then it adds each of the query parameters found in the query string to this route. It does this because, for some reason, the current request's RouteValueDictionary does not contain them (you'd think it would, but it doesn't).</p> <p>You can then take the resultant dictionary, modify only a part of it, for example:</p> <pre><code>routeValues["page"] = 2; </code></pre> <p>and then give that dictionary to the out-of-the-box HtmlHelper methods for them to generate you a URL/etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload