0

我的页面上有分页列表,它允许 5 个页面查看结果,其格式为 www.example.com/viewing?1 ... 2 ...3 一直到 5,我正在检查我是哪个页面在做

@if (Model.article.PageNumber == 1)
{ 
}

然后

@if (Model.article.PageNumber == 2)
{ 
}

一直到 5 .. 有没有更好的方法来做到这一点这里是代码..

// look at Model.article.PageNumber as you can see I use that to get pages 1-5 and
// it keeps the tabs in order if page=2 then the first element is page2 -1 and the like
// how can i reduce this code so i can add the page numbers correctly without repeating 5 times

@if (Model.article.PageNumber == 1)
{ 
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber).ToString()), "index", new { page = Model.article.PageNumber })  @Html.Raw(" ");          
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 1).ToString()), "index", new { page = Model.article.PageNumber + 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 2).ToString()), "index", new { page = Model.article.PageNumber + 2 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 3).ToString()), "index", new { page = Model.article.PageNumber + 3 })@Html.Raw(" ");  
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 4).ToString()), "index", new { page = Model.article.PageNumber + 4 }) 
}

@if (Model.article.PageNumber == 2)
{  
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 1).ToString()), "index", new { page = Model.article.PageNumber - 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber).ToString()), "index", new { page = Model.article.PageNumber }) @Html.Raw(" ");      
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 1).ToString()), "index", new { page = Model.article.PageNumber + 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 2).ToString()), "index", new { page = Model.article.PageNumber + 2 })@Html.Raw(" "); 
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 3).ToString()), "index", new { page = Model.article.PageNumber + 3 })
    @Html.Raw(" ");
}

@if (Model.article.PageNumber == 3)
{  
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 2).ToString()), "index", new { page = Model.article.PageNumber - 2 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 1).ToString()), "index", new { page = Model.article.PageNumber - 1 })@Html.Raw(" ");       
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber).ToString()), "index", new { page = Model.article.PageNumber })@Html.Raw(" "); 
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 1).ToString()), "index", new { page = Model.article.PageNumber + 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 2).ToString()), "index", new { page = Model.article.PageNumber + 2 })  
}

@if (Model.article.PageNumber == 4)
{  
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 3).ToString()), "index", new { page = Model.article.PageNumber - 3 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 2).ToString()), "index", new { page = Model.article.PageNumber - 2 })  @Html.Raw(" ");     
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 1).ToString()), "index", new { page = Model.article.PageNumber - 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber).ToString()), "index", new { page = Model.article.PageNumber }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber + 1).ToString()), "index", new { page = Model.article.PageNumber + 1 })  @Html.Raw(" ");
}

@if (Model.article.PageNumber == 5)
{  
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 4).ToString()), "index", new { page = Model.article.PageNumber - 4 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 3).ToString()), "index", new { page = Model.article.PageNumber - 3 })    @Html.Raw(" ");   
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 2).ToString()), "index", new { page = Model.article.PageNumber - 2 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber - 1).ToString()), "index", new { page = Model.article.PageNumber - 1 }) @Html.Raw(" ");
    @Html.ActionLink(String.Format("{0}", (Model.article.PageNumber).ToString()), "index", new { page = Model.article.PageNumber })  
}
4

2 回答 2

1

既然您将自己限制为 5 页,为什么需要根据当前页码使用大的 if/else 语句动态生成页脚或其他内容?您是对的,因为这可能会很快变得不守规矩,特别是如果您决定将来在文章中添加更多页面。

看来您可以传递单个属性,例如记录计数或 numPages 并执行以下操作:

@{for(var pageNum = 1; pageNum <= Model.article.numPages, pageNum++){
    @Html.ActionLink(string.Format("{0} ", pageNum), "index", new { page = pageNum })                
}}

这将减少我在上面看到的嵌套,消除最后对 Html.Raw 的需求并且与页面无关(这意味着你在哪个页面上并不重要)。您还可以在视图模型中包含当前请求的页面,这样您就无法将该页面设置为链接。例如:

@{for(var pageNum = 1; pageNum <= Model.article.numPages, pageNum++){
    if(pageNum <> Model.article.PageNumber){
       @Html.ActionLink(string.Format("{0} ", pageNum), "index", new { page = pageNum })
    }else{
       @Model.article.PageNumber
    } 
}}
于 2013-02-09T06:12:47.243 回答
0

考虑使用我的 PagedList nuget 包:

https://github.com/troygoode/pagedlist

于 2013-02-09T16:21:27.330 回答