我的问题是这个问题的延续:
Filter and Pagination in MVC 4
在开始的代码块中:
<div>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
我已经在我的项目中实现了它,它非常适合创建页面和适当的导航链接。但它没有从数据库中提取我的实际列表;只是页面链接。
以下是我之前在Index.cshtml文件中的内容:
@model IEnumerable<PtoTracker.Employee>
@{
ViewBag.Title = "Employees";
}
<h2>Employees</h2>
<p>
@Html.ActionLink("Add New Employee", "Create")
</p>
<table>
<tr>
<th></th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new { id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID })
</td>
</tr>
}
</table>
上面的代码效果很好,但它只是一个很长的员工名单。如何将上面的代码与此处显示的分页部分合并:
<div>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort });
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort });
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort });
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort });
}
else
{
@:Next >
@Html.Raw(" ");
@:>>
}
</div>
答案 0 :(得分:1)
网格和分页的另一个很好的选择:MvcContrib Grid
答案 1 :(得分:1)
我明白了。我只需要将第一行从@model IEnumerable...
更改为@model PageList.IPagedList...
,然后将两个块一起打开。
这是我的工作代码:
@model PagedList.IPagedList<PtoTracker.Employee>
@{
ViewBag.Title = "Employees";
}
<h2>Employees</h2>
<p>
@Html.ActionLink("Add New Employee", "Create")
</p>
<table>
<tr>
<th></th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new { id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID })
</td>
</tr>
}
</table>
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort });
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort });
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort });
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort });
}
else
{
@:Next >
@Html.Raw(" ");
@:>>
}