views:

68

answers:

3

the very famous actionlink:

 <%: Html.ActionLink("Back to List", "Index")%>

Now, this link is in my Details view. The Index view is a search page. The url of that looks like this:

http://localhost:50152/2011-2012/Instelling/Details/76?gemeente=Dendermonde&amp;postcode=92**&amp;gebruikerscode=VVKSO114421&amp;dossiernr=114421%20&amp;organisatie=CLB

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 ActionLink.

now, I'm tired of doing that manually, it's ok for 1, but not for 6. This should go a lot easier.

question: How do I return All parameters of the current url into the actionlink as optional routevalues.

I've been looking to Request.QueryString. It has to be something with that. I was thinking of writing some static method in Global.asax doing the job but no luck yet. maybe there is an easy way to do this which I dont know about??

Edit: This is what I came up with (which works)

In global.asax:

    public static RouteValueDictionary optionalParamters(NameValueCollection c) {
        RouteValueDictionary r = new RouteValueDictionary();
        foreach (string s in c.AllKeys) {
            r.Add(s, c[s]);
        }
        return r;
    }

Details.aspx:

    <%: Html.ActionLink("Back to List", "Index", MVC2_NASTEST.MvcApplication.optionalParamters(Request.QueryString))%>

where do I best put this code? not in global.asax I guess...

Edit 2:

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

    }
}


<%@ Import Namespace="MVC2_NASTEST.Helpers" %>
...
<%: Html.ActionLinkwParams("Index") %>
+2  A: 

perhaps the best way is to write ur own html helper where u traverse through previous route value dictionary and add route values to current action link except the action parameter off course. edit: you can write html helper like

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
}
Muhammad Adeel Zahid
RouteValueDictionary is exactly what I needed. But how would you make that helper then?
Stefanvds
@Stefanvds: You would call it with `<%= Html.CustomLink("Foo") %>`. P.S. @Muhammad - What's with all the txt spk? We're not constrained to 160 chars here, you know!
Dan Atkinson
twitter is 140 char :P
Stefanvds
MvcHtmlString is depracated
Stefanvds
I need more info on this thing, I dont get it. some articles could be helpful. error: 'System.Web.Mvc.HtmlHelper<MVC2_NASTEST.Models.DetailedInstelling>' does not contain a definition for 'ActionLinkwParams' and no extension method 'ActionLinkwParams' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<MVC2_NASTEST.Models.DetailedInstelling>' could be found (are you missing a using directive or an assembly reference?
Stefanvds
ah sorry plz use string in place of MvcHtmlString
Muhammad Adeel Zahid
plus u need to add namespace in ur veiw to use custom html helpers plz c http://stackoverflow.com/questions/1105276/custom-htmlhelper-extension-method-not-available-in-view for more detail
Muhammad Adeel Zahid
i did import it. the problem is with this HtmlHelper.
Stefanvds
can u plz paste ur code
Muhammad Adeel Zahid
helper.ViewContext.RouteData.Values is useless, it contains the values from my route, obviously the optional paramters are not in my route... so they are not there!
Stefanvds
sorry today was too busy i have written some code adding in new answer
Muhammad Adeel Zahid
A: 

This is how I finally fixed it, and i'm rather proud because it's working very well and very DRY.

The call in the View:

    <%: Html.ActionLinkwParams("Back to List", "Index")%>

but with the overloads it can be anything which a normal ActionLink takes.

The Helper:

The helper takes all parameters from the url which are not in the route. For example: this url:

http://localhost:50152/2011-2012/myController/Details/77?postalCode=9***&amp;org=CLB

So it will take the postalCode and the Org and place it in the new ActionLink. With the overload, additional parameters can be added, and parameters from the existing url can be removed.

using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace MVC2_NASTEST.Helpers {
    public static class ActionLinkwParamsExtensions {
        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs, object htmlAttributes) {

            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }

            RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = Merge(r, extra);

            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, linktext, action, controller, m, htmlAtts);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action) {
            return ActionLinkwParams(helper, linktext, action, null, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller) {
            return ActionLinkwParams(helper, linktext, action, controller, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, controller, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs, object htmlAttributes) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, htmlAttributes);
        }




        static RouteValueDictionary Merge(this RouteValueDictionary original, RouteValueDictionary @new) {

            // Create a new dictionary containing implicit and auto-generated values
            RouteValueDictionary merged = new RouteValueDictionary(original);

            foreach (var f in @new) {
                if (merged.ContainsKey(f.Key)) {
                    merged[f.Key] = f.Value;
                } else {
                    merged.Add(f.Key, f.Value);
                }
            }

            return merged;

        }
    }

}

In the View using overloads:

 <%: Html.ActionLinkwParams("Back to List", "Index","myController", new {testValue = "This is a test", postalCode=String.Empty}, new{ @class="test"})%>

in the URL I have the paramters postalCode with some value. my code takes All of them in the URL, by setting it to string.Empty, I remove this parameter from the list.

Comments or ideas welcome on optimizing it.

Stefanvds
A: 
public static class Helpers
    {
        public static MvcHtmlString CustomLink(this HtmlHelper helper,string LinkText, string actionName)
        {
            var rtvals = helper.ViewContext.RouteData.Values;
            var rtvals2 = helper.RouteCollection;
            RouteValueDictionary rv = new RouteValueDictionary();
            foreach (string param in helper.ViewContext.RequestContext.HttpContext.Request.QueryString.AllKeys) 
            {
                rv.Add(param, helper.ViewContext.RequestContext.HttpContext.Request.QueryString[param]);
            }
            foreach (var k in helper.ViewContext.RouteData.Values) 
            {
                rv.Add(k.Key, k.Value);
            }
            return helper.ActionLink(LinkText, actionName, rv);
        }
    }

i have tested this and its working. optional parameters can be acquired from query string HTH

Muhammad Adeel Zahid