代码之家  ›  专栏  ›  技术社区  ›  mattruma

ASP.NET MVC中的分页问题

  •  1
  • mattruma  · 技术社区  · 15 年前

    我正在尝试实现Nerddiner ASP.NET中使用的相同分页。每当分页开始时,我在我的视图中接收到以下错误。

    “名为”index“的路由无法 在路由集合中找到。“

    错误发生在第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>
    

    我的控制器代码是:

    [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);
    }
    

    我的视图代码是…

    <%@ 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>
    

    这是我第一次尝试在ASP.NET MVC中进行分页…如果有更好的方法,请告诉我,否则,我在哪里出错?

    多谢!

    2 回复  |  直到 15 年前
        1
  •  3
  •   HakonB    15 年前

    您不应该使用routelink(采用路由名称),而是使用actionlink(采用类似index的操作名称)。

        2
  •  2
  •   JOBG    15 年前

    那么,routeLink扩展方法在global.asax中查找一个名为“index”的已定义路由,默认情况下,在global中只定义了一个路由“default”,如下所示:

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

    也许正如Hakonb所说,您必须使用actionLink扩展方法,或者在全局asax中为分页定义一个路由。