Pagination-asp.net mvc 4

时间:2013-03-18 02:57:18

标签: asp.net-mvc asp.net-mvc-4

我在显示有关如何包含prev和next链接的页面时遇到问题... 我正在使用ASP.Net MVC 4使用这些代码:

查看:

<div class="pagination">
    <ul>
        @for (int i = 1; i <= ViewBag.PagesCount; i++)
        {
            <text>
                <li>@Html.ActionLink(i.ToString(), "Index", new { pagee = i })</li>
            </text>
        }
    </ul>
</div>

控制器:

int offset = 15;

int pagecount = ((pagee - 1) * offset);

int totalPages = 0;
int totalItems = 0;

try
{       
    totalItems = requestform.Count();
    while (totalItems > 0)
    {
        totalItems -= 15;
        totalPages++;
    }
    ViewBag.PagesCount = totalPages;
    return View(requestform.Skip(pagecount).Take(offset).ToList());
}
catch
{
    while (totalItems > 0)
    {
        totalItems -= 15;
        totalPages++;
    }
    ViewBag.PagesCount = totalPages;
    return View(requestform.Skip(pagecount).Take(offset).ToList());
}

请帮助我。

4 个答案:

答案 0 :(得分:2)

你对这个小延伸PagedList怎么说?

答案 1 :(得分:0)

要向页面添加分页,首先要安装PagedList NuGet包。然后,您将在Index方法中进行其他更改,并将分页链接添加到Index视图

请检查此链接 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

答案 2 :(得分:0)

我建议PagedList只需通过NuGet包管理器添加它,然后

将您的控制器修改为如下所示:

public ActionResult Index(int? page)
        {
            var dbtable = db.DbTable.Include(s => s.ID).OrderBy(s => s.StyleName); //ensure records are sorted.
            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }
            int pageSize = 2;
            int pageNumber = (page ?? 1);
            return View(dbtable.ToPagedList(pageNumber, pageSize));
        }

在视图中替换:

@model IEnumerable<MyMvcApplication.Models.dbtable>

@model PagedList.IPagedList<MyMvcApplication.Models.dbtable>

替换看起来像

的代码
    <th>
            @Html.DisplayNameFor(model => model.ID)
        </th>

<th>
            @Html.DisplayNameFor(model => model.First().ID)
        </th>

</table>标记后的页面末尾添加:

<div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "Index", new { page = 1})
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "Index", new {page = Model.PageNumber - 1})
    }
    else{
     @:<<
     @Html.Raw(" ");
        @:< Prev   
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "Index", new {page = Model.PageNumber + 1})
        @Html.Raw(" "); 
        @Html.ActionLink(">>", "Index", new {page = Model.PageCount})
    }
    else{
     @:Next >
     @Html.Raw(" ")
@:>>   
    }
</div>

答案 3 :(得分:0)

最近我发现酷控件,工具提示,第一个,下一个,最后一个,上一个检查此mvc 4 paging nuget package,演示web page