Reputation: 77379
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 :)
Upvotes: 1
Views: 1816
Reputation: 39290
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"] }))
Upvotes: 1
Reputation: 25323
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.
Upvotes: 1