Question

I have an action called List that shows the results of a search. It receives parameters through the querystring because they are optional. My method signature looks like this:

public ActionResult List(List<int> categoryIDs, string school, int? stateID, int? page)

CategoryIDs is a multi-select box and I am doing everything over a GET request. What I need to do is create a link in my view to the next page but retain the same search parameters. I know I can build the link by hand but it is possible to use any of the built-in routing method especially when the categoryIDs have to be formatted like "?categoryID=1&categoryID=2&categoryID=3" to be bound to the list?

Was it helpful?

Solution

I think there's no ActionLink overload that helps you do that by default. You need to populate the RouteValueDictionary instance with the parameters you want to include.

For the list of category, try s/t like categoryIDs=2,3,4,5 etc. since repeating keys are not allowed in RouteValueDictionary. After that, in the action method will need to parse the string into the integer list.

OTHER TIPS

You should be able to do this:

All the current values should be passed in by default. I'd have to try it out though to make sure I'm referencing the correct overload.

I like to pass an object as a parameter to search actions, and then pass the parameter object to the view. So with some code in your controller like this (note I'm using Rob Conery's PagedList class):

public class SearchParameters {
    public string School { get; set; }
    public int? StateID { get; set; }
    public int? Page { get; set; }

    public SearchParameters GetPage(int page) {
        return new SearchParameters {
            School = School,
            StateID = StateID,
            Page = page
        };
    }
}

public class SearchViewModel {
    public PagedList<[YourResultType]> Results { get; set; }
    public SearchParameters Parameters { get; set; }
}

// ...

public ActionResult Search(SearchParameters parameters) {
    IQueryable<[YourResultType]> query;
    // ... do some stuff to get your search results

    return View("Search", new SearchViewModel {
        Results = query.ToPagedList(parameters.Page - 1), 15),
        Parameters = parameters
    });
}

So your search view inherits from the generic ViewPage<T> with a page declaration like this:

<%@ Page ... Inherits="ViewPage<SearchViewModel>" %>

Then in your search view, you can do something like this...

<% for(int i = 0; i < Model.Results.TotalPages; i++) { %>
    <%= Html.ActionLink(i + 1).ToString(), "Search",
        Model.Parameters.GetPage(i + 1)) %>
<% } %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top