我的页面上有分页列表,它允许 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 })
}