1

I'm trying to implement some simple paging, based on How do I do pagination in ASP.NET MVC?

The paging works fine.

However, I'm now trying to create previous and next links, but can't figure out how to access the params:

My route looks like:

  routes.MapRoute(
      "Name",
      "Controller/ActionName/{pageID}",
      new { controller = "Controller", action = "ActionName" , pageID = 0 },
      new { pageID = @"\d*"}
      );

And my next link looks like:

   <%=Html.ActionLink("next page", "ActionName", "Controller", new {pageID = pageID + 1 }, null) %>

The error I get is:

 Compiler Error Message: CS0103: The name 'pageID' does not exist in the current context

How should I create the Prev/Next links (or, in this case, just the next)?

Community
  • 1
  • 1
chris
  • 36,094
  • 53
  • 157
  • 237

1 Answers1

1

The error is occurring on the second PageID in

new {pageID = pageID + 1 }, ...

If you want to reference pageID in this way, you have to pass it in as part of your model.

Have a look at the following tutorial:

NerdDinner Step 8: Paging Support
http://nerddinnerbook.s3.amazonaws.com/Part8.htm

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Is it still possible to use this approach using a strongly-typed views? I'm getting an error saying 'System.Collections.Generic.IEnumerable' does not contain a definition for 'HasPreviousPage' – chris Dec 09 '09 at 17:19
  • Look closely at the model NerdDinner is using. There is more than one object in there. – Robert Harvey Dec 09 '09 at 17:30
  • So what it comes down to is that I can't do what I want to do. I have no real issues with using ViewData, I was just hoping there was a simple way to access the route variables. Thanks. – chris Dec 09 '09 at 17:33
  • To access the route variables, use `this.ViewContext.RouteData`. http://stackoverflow.com/questions/1134776/how-do-you-access-the-current-routedata-from-view-in-net-mvc – Robert Harvey Dec 09 '09 at 17:53