views:

236

answers:

1

I have a search page which allows users to further filter thier results based on criteria within a certain set.

You start a search by searching for all items within a "tag". The URL created for this would look like

search/index?tag=TagA

On the page there are a list of tags that are also in this result set.

What I want is so in this list of tags the URL's generated are

<a href="search/index?tag=TagA,TagB">TagB</a>

It's not good enough just to append onto the URL as there will be other parameters added such as page numbers and other search criteria (I have not included them for brevity)

I'm aware I could probably hack this on the server side but nothing feels very elegant and I was wondering if there was a neat solution for this.

This is all done in ASP MVC and as such I have a nice simple partial view to list these tags:

<%if(Model.Count()>0){ %>    
    <ul>
    <%foreach(Tag t in Model){ %>
        <li><%=t.name%></li>
    <%} %>
    </ul> 
<%} %

Any ideas?

+1  A: 

This ain't good looking code, but it might give you an idea:

  public static string CurrentUrl
        (this UrlHelper helper)
    {
        return GenerateUrl
            (helper, GetParameters(helper), null, null);
    }

    public static string CurrentUrlWith
        (this UrlHelper helper, string key, string value)
    {
        return GenerateUrl
            (helper, GetParameters(helper), key, value);
    }



 private static Dictionary<string, object> GetParameters
            (UrlHelper helper)
        {
            var context = helper.RequestContext.HttpContext;
            var request = context.Request;
            var parameters = new Dictionary<string, object>();
            request.Form.CopyTo(parameters);
            request.QueryString.CopyTo(parameters);

            return parameters;
        }

        //TODO: refactor
        private static string GenerateUrl
            (UrlHelper helper, Dictionary<string, object> parameters,
             string key, string value)
        {
            var context = helper.RequestContext.HttpContext;
            var request = context.Request;
            string query = "?",
                   url = request.FilePath;

            foreach (var parameter in parameters)
                if (parameter.Key != key)
                    query += string.Format("&{0}={1}",
                        helper.Encode(parameter.Key), 
                        helper.Encode(parameter.Value.ToString()));

            if (key != null && value != null)
                query += string.Format("&{0}={1}", helper.Encode(key), helper.Encode(value));

            if (query.Length > 1)
                query = query.Remove(1, 1);

            return url + query;
        }

Usage looks like this:

<a href="<%= Url.CurrentUrlWith("page", (Model.TotalPages).ToString())%>" 
    class="p-last">Last page</a>

If you want to involve javascript, you might want to check out this jQuery plugin.

Arnis L.
This has got me on the right track, thanks :)
qui