Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC ActionLink add all (optional) parameters from current url
    primarykey
    data
    text
    <p>The very famous <code>ActionLink</code>:</p> <pre><code> &lt;%: Html.ActionLink("Back to List", "Index")%&gt; </code></pre> <p>Now, this link is in my Details view. The Index view is a search page. The URL of that looks like this: </p> <pre><code>http://localhost:50152/2011-2012/Instelling/Details/76?gemeente=Dendermonde&amp;postcode=92**&amp;gebruikerscode=VVKSO114421&amp;dossiernr=114421%20&amp;organisatie=CLB </code></pre> <p>As you can see, quite the amount of parameters. Obviously I want to keep all these parameters when I return to the Index page, so I need to add them in the <code>ActionLink</code>.</p> <p>Now, I'm tired of doing that manually, it's ok for 1, but not for 6. This should go a lot easier. </p> <p><strong>Question: How do I return All parameters of the current URL into the <code>ActionLink</code> as optional <code>RouteValues</code>.</strong></p> <p>I've been looking to <code>Request.QueryString</code>. It has to be something with that. I was thinking of writing some static method in <code>Global.asax</code> doing the job but no luck yet. Maybe there is an easy way to do this which I don't know about?</p> <p><strong>Edit:</strong> This is what I came up with (which works)</p> <p>In <strong>global.asax:</strong></p> <pre><code> public static RouteValueDictionary optionalParamters(NameValueCollection c) { RouteValueDictionary r = new RouteValueDictionary(); foreach (string s in c.AllKeys) { r.Add(s, c[s]); } return r; } </code></pre> <p><strong>Details.aspx:</strong></p> <pre><code> &lt;%: Html.ActionLink("Back to List", "Index", MVC2_NASTEST.MvcApplication.optionalParamters(Request.QueryString))%&gt; </code></pre> <p>Where do I best put this code? not in <code>Global.asax</code> I guess...</p> <p><strong>Edit 2:</strong></p> <pre><code>using System; using System.Web.Mvc; namespace MVC2_NASTEST.Helpers { public static class ActionLinkwParamsExtensions { public static MvcHtmlString CustomLink(this HtmlHelper helper, string linktext) { //here u can use helper to get View context and then routvalue dictionary var routevals = helper.ViewContext.RouteData.Values; //here u can do whatever u want with route values return null; } } } &lt;%@ Import Namespace="MVC2_NASTEST.Helpers" %&gt; ... &lt;%: Html.ActionLinkwParams("Index") %&gt; </code></pre>
    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.
 

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