Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have the following scenario:

1) A phone book table

2) Multiple ways of searching through the table (first name, last name)

3) There is also the obligatory paging (page size and page number) as well as sorting

When the user accesses the page first the Url is simply /Phonebook. Now let's say the user is searching for "abc", then the Url becomes /Phonebook?q=abc. Then he is sorting ascending by first name, and the Url should become /Phonebook?q=abc&sort=firstName&order=asc.

The problem I have is how do I program my view to automatically append/modify the query string so that it still contains previously entered constraints (as in my "abc") query but adjusts/adds new constraints for example if sorting, and then paging etc. is used?

I'd hate to use JS and rewriting the location for this, and rather have the page generate real anchors (a href) after every postback for each sort/page/filter link on my page.

Appreciate your time folks :)

share|improve this question
    
updated with some examples for you – Robert MacLean Jul 22 '09 at 21:05
up vote 1 down vote accepted

I assume you have some sort of controls on the page to set what will be sorted, in what order and obviously a way to define what will filtered?

When you load the page, why not set those values in the view and then the controller should update correctly on the next load? That way it is easy for the user to change them as well without needing to touch the URL. In this case it is straight forward passing the data from the controller to the view and setting the values as you would any other control, for instance:

<input type="text" name="search" value="<%= (string)ViewData["Search"] %>" />

If you really need it in the URL, you can modify the button by passing the extra parameters in an anonymous type.

For a link something like this:

<%= Url.Action("Search", "PhoneBook", new {order="asc"}) %>

For a form (since you don't specifically set a submit button) something like this

<% using (Html.BeginForm(new {order="asc"}) {

You could pull those from the controller by doing something like

<% using (Html.BeginForm(new { sort=(string)ViewData["Sort Order"] }))
share|improve this answer
    
Could you add an example please? Thank you :) – Alex Jul 22 '09 at 20:28

Just pass extra parameters in with an anonymous object:

<%= Html.ActionLink("Sort ascending", "Search", "PhoneBook", new {order="asc"}) %>
<!-- or -->
<%= Url.Action("Search", "PhoneBook", new {order="asc"}) %>

If the extra parameters are not defined in the route, they will be added as query strings.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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