2009-11-20 49 views
1

我想實現在NerdDinner ASP.NET中使用相同的分頁。我在我看來,只要分頁開始在踢收到以下錯誤。在ASP.NET MVC分頁問題

「命名的‘索引’的路由不能 路由集合中找到。」

的錯誤是在發生行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中執行分頁...如果有更好的wa Ÿ,請讓我知道,否則,我在哪裏錯了?

非常感謝!

回答

3

您不應該使用RouteLink(它採用路由名稱),而是使用一個ActionLink,它採用索引等操作名稱。

+0

這樣做的竅門!想知道爲什麼以及他們如何在NerdDinner示例中使用RouteLink? – mattruma 2009-11-20 15:11:00

2

那麼RouteLink擴展方法在Global.asax中查找名稱爲「Index」的已定義路由,默認情況下全局中僅定義了一條「Default」路由,如下所示:

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

也許因爲HakonB說,你必須使用ActionLink的擴展方法或在分頁全球ASAX定義路由。

+0

+1謝謝你的解釋! – mattruma 2009-11-20 20:00:39