0

我的问题是这个问题的延续:

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>
4

2 回答 2

1

网格和分页的另一个很好的选择:MvcContrib Grid

于 2012-11-16T17:30:11.593 回答
1

我想到了。我只需要将第一行从 to 更改为@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(" ");
    @:>>
}

于 2012-11-16T18:14:10.987 回答