views:

363

answers:

1

This seems like it should be pretty straight forward, but I'm apparently confused.

I have a List view that displays a paged list. At the bottom I have a set of actionlinks:

<%= Html.ActionLink("First Page", "List", new { page = 1} ) %>
&nbsp; 
<%= Html.ActionLink("Prev Page", "List", new { page = Model.PageNumber - 1 }) %>
&nbsp;
<%= Html.ActionLink("Next Page", "List", new { page = Model.PageNumber + 1 }) %>
&nbsp;
<%= Html.ActionLink("Last Page", "List", new { page = Model.LastPage } )%>

I'm using the basic default routes setup, except with "List" substituted for "Index":

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "List", id = UrlParameter.Optional } // Parameter defaults
        );

The problem is that the ActionLink helpers are generating links of the form:

http://localhost:2083/Retrofit?page=2

rather than

http://localhost:2083/Retrofit/?page=2

(with the trailing slash after the controller name & before the query string). When the first URL is routed, it completely loses the query string - if I look at Request.QueryString by the time it gets to the controller, it's null. If I enter the second URL (with the trailing slash), it comes in properly (i.e., QueryString of "page=2").

So how can I either get the ActionLink helper to generate the right URL, or get the Routing to properly parse what ActionLink is generating?

Thanks.

A: 

You need to choose one of two actions:

  1. Modify your route to take a page parameter instead of the default id, and modify the signature of your action method accordingly.

  2. Modify the calls to Html.AcitonLink() so they name the parameter id - that is, change new { page = ... } to new { id = ... }.

Tomas Lycken
Tomas,That did do it. It now generates a link of "http://localhost:2083/Retrofit/List/1.However, that wasn't what I wanted. What I want is to have the page number generated as a query string: "http://localhost:2803/Retrofit/?page=<n>". I need to have both an optional id (for viewing/editing details of an item) given in the path of the URL, AND an optional page number given as a query string. How do I do that?
Dave Hanna