Pagination issues in ASP.NET MVC

I am trying to implement the same page used by NerdDinner ASP.NET. I get the following error, in my opinion, whenever a pagination begins.

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

Error 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:

[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 ...

<%@ 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 to do pagination in ASP.NET MVC ... if there is a better way, let me know, otherwise, where am I not here?

Thanks a lot!

+1
source share
2 answers

RouteLink ( ), ActionLink, , Index.

+3

, RouteLink "Index" Global.asax, Global , "Default", :

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

, HakonB , ActionLink asax .

+2

All Articles