mattruma
mattruma

Reputation: 16677

Issues with pagination in ASP.NET MVC

I am trying to implementation the same pagination that is used in the NerdDinner ASP.NET. I am receiving the following error in my view, whenever the pagination starts to kick in.

"A route named 'Index' could not be found in the route collection."

The error is happening on Line 64.

Line 62:         <% if (this.Model.HasNextPage)
Line 63:            { %>
Line 64:         <%= this.Html.RouteLink("Next Page >>>", "Index", new { page = (this.Model.PageIndex + 1) })%>
Line 65:         <% } %>
Line 66:     </div>

My controller code is:

[Authorize]
public ActionResult Index(int? page)
{
    const int pageSize = 25;

    var topics = this.TopicRepository.FindAllTopics();
    var paginatedTopics = new PaginatedList<Topic>(topics, page ?? 0, pageSize);

    return this.View(paginatedTopics);
}

My view code is ...

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CreativeLogic.Sauron.WebMvc.Helpers.PaginatedList<CreativeLogic.Sauron.WebMvc.Models.Topic>>" %>

<!-- Code to display the list here -->

<div class="pagination">
    <% if (this.Model.HasPreviousPage)
       { %>
    <%= this.Html.RouteLink("<<< Previous Page", 
                           "Index", new { page = (this.Model.PageIndex - 1) }) %>
    <% } %>
    <% if (this.Model.HasNextPage)
       { %>
    <%= this.Html.RouteLink("Next Page >>>", 
                           "Index", new { page = (this.Model.PageIndex + 1) })%>
    <% } %>
</div>

This is my first attempt at doing the pagination in ASP.NET MVC ... if there is a better way, please let me know, otherwise, where am I going wrong here?

Thanks much!

Upvotes: 1

Views: 1077

Answers (2)

JOBG
JOBG

Reputation: 4624

Well the RouteLink Extension Method looks for a defined route with the name of "Index" in the Global.asax, and by default there is just 1 route defined in the Global the "Default", it looks like this:

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

Maybe as HakonB said you must use the ActionLink Extension Method or define a route in the Global asax for the pagination.

Upvotes: 2

HakonB
HakonB

Reputation: 7075

You should not use RouteLink (which takes a route name) but instead use an ActionLink which takes an action name like Index.

Upvotes: 3

Related Questions