vote up 0 vote down
star
1

I want to have different sorting and filtering applied on my view I figured that i'll be passing sorting and filtering params through query string

<%= Html.ActionLink("Name", "Index", new { SortBy= "Name"}) %>

this simple construction allows me to sort.

view comes back with

?SortBy=Name

in query string

now i want to add filtering and i want my query strig to end up with

?SortBy=Name&Filter=Something

how can i add another parameter to list of already existing ones in ActionLink?

example:

user requests /Index/

view has

 <%= Html.ActionLink("Name", "Index", new { SortBy= "Name"}) %>

and

 <%= Html.ActionLink("Name", "Index", new { FilterBy= "Name"}) %>

links

first one looks like /Index/?SortBy=Name second is /Index/?FilterBy=Name

i want when user pressed sorting link after he applied some filtering - filtering is not lost, so i need a way to combine my params. My guess is there should be a way to not parse query string, but get collection of parameters from some mvc object.

flag
add comment

2 Answers:

vote up 3 vote down
check
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name", Filter="Something"}) %>

To preserve the querystring you can:

<%= Html.ActionLink("Name", "Index", 
     String.IsNullOrEmpty(Request.QueryString["SortBy"]) ? 
        new { Filter = "Something" } : 
        new { SortBy=Request.QueryString["SortBy"], Filter="Something"}) %>

Or if you have more parameters, you could build the link manually by using taking Request.QueryString into account.

link|flag
add comment
vote up 1 vote down

so far the best way i figured out is to create a copy of ViewContext.RouteData.Values and inject QueryString values into it. and then modify it before every ActionLink usage. still trying to figure out how to use .Union() instead of modifying a dictionary all the time.

<% RouteValueDictionary   tRVD = new RouteValueDictionary(ViewContext.RouteData.Values); %>

<% foreach (string key in Request.QueryString.Keys )
    {
    	 tRVD[key]=Request.QueryString[key].ToString();
    } %>

<%tRVD["SortBy"] = "Name"; %>
                <%= Html.ActionLink("Name", "Index", tRVD)%>
link|flag
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.