【发布时间】:2018-02-02 15:53:54
【问题描述】:
我目前正在 c# asp.net 中为我的简单项目创建自定义分页,到目前为止一切顺利,但“下一步”和“最后一个”按钮无法正常工作。 “下一页”按钮只返回当前页面,或者如果我在另一个大于 1 的页面,它会返回 1 页。
“最后一页”按钮未按预期转到最后一页。
我知道这只是视图中 for 循环中的某些内容,或者我在模型文件中缺少某些内容。
到目前为止,这是我的代码:
Index.cshtml(视图)
@model FinalPaginationTask.Models.UserCustom
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Content/***.css" />
<title> </title>
</head>
<body>
<h2>List of Users</h2>
@using (Html.BeginForm(null, null, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.Hidden("SortField", Model.SortField)
@Html.Hidden("SortDirection", Model.SortDirection)
@Html.Hidden("PageCount", Model.PageCount)
@Html.Hidden("PageSize", Model.PageSize)
@Html.Hidden("CurrentPageIndex", Model.CurrentPageIndex)
@*@Html.Hidden("SelectedUsr_Id", Model.SelectedUsr_Id)
@Html.Hidden("SelectedUsername", Model.SelectedUsername)*@
<table id="customers" class="table-striped">
<tr>
<th >
<a href="#" data-sortfield="usr_Id" class="header">@Html.DisplayNameFor(model => model.user.First().usr_Id)</a>
</th>
<th>
<a href="#" data-sortfield="username" class="header">@Html.DisplayNameFor(model => model.user.First().username)</a>
</th>
<th>
@Html.DisplayNameFor(model => model.user.First().email)
</th>
<th>
</th>
</tr>
<tr>
@*<th>
@Html.DropDownListFor(model => model.SelectedUsr_Id,
new SelectList(Model.userDDL, "usr_Id", "usr_Id", Model.SelectedUsr_Id), "All", new { @id = "fn" })
</th>
<th>
@Html.DropDownListFor(model => model.SelectedUsername,
new SelectList(Model.userDDL, "username", "username", Model.SelectedUsername), "All", new { @id = "ln" })
</th>*@
</tr>
@foreach (var item in Model.user)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.usr_Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.username)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
<td>
@Html.ActionLink("Create User", "CreateUser", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
<tr></tr>
@*@if (Model.Pager.LastPage > 1)
{*@
<tr class="pagination">
@if (Model.CurrentPageIndex >= 1)
{
<td class="pager"><a href="~/User/">First</a></td>
<td class="pager"><a href="~/User/Index/@(Model.CurrentPageIndex - 1)">Previous</a></td>
}
@for (var i = 0; i < Model.PageCount; i++)
{
if (i == Model.CurrentPageIndex)
{
<td class="pager"><span class="current-pager" id="CurrentPageIndex">@(i + 1)</span></td>
}
else
{
<td class="pager" ><a href="~/User/Index/@i" data-pageindex="@i" class="pager">@(i + 1)</a></td>
}
}
@if (Model.CurrentPageIndex < Model.PageCount)
{
<td class="pager"><a href="~/User/Index/@(Model.CurrentPageIndex + 1)">Next</a></td>
<td class="pager"><a href="~/User/Index/@(Model.PageCount)">Last</a></td>
}
</tr>
@*}*@
</table>
}
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
$(".header").click(function (evt) {
var sortfield = $(evt.target).data("sortfield");
if ($("#SortField").val() == sortfield) {
if ($("#SortDirection").val() == "ascending") {
$("#SortDirection").val("descending");
}
else {
$("#SortDirection").val("ascending");
}
}
else {
$("#SortField").val(sortfield);
$("#SortDirection").val("ascending");
}
evt.preventDefault();
$("form").submit();
});
$(".pager").click(function (evt) {
var pageindex = $(evt.target).data("pageindex");
$("#CurrentPageIndex").val(pageindex);
evt.preventDefault();
$("form").submit();
});
$("#fn").change(function (evt) {
$("#SelectedUsr_Id").val($("#fn").val().trim());
evt.preventDefault();
$("form").submit();
})
$("#ln").change(function (evt) {
$("#SelectedUsername").val($("#ln").val().trim());
evt.preventDefault();
$("form").submit();
})
});
</script>
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FinalPaginationTask.Models
{
public class SortAndPage // used for sorting and paging
{
public string SortField{ get; set; }
public string SortDirection { get; set; }
public int PageSize { get; set; }
public int PageCount { get; set; }
public int StartPage { get; set; }
public int LastPage { get; set; }
public int CurrentPageIndex { get; set; }
public Pager Pager { get; set; }
}
}
任何帮助将不胜感激。非常感谢!
编辑: 我将添加我的控制器,因为可能需要其他信息:
public ActionResult Index(UserCustom userCustomModel = null)
{
// start load customer
//int i;
//if (userCustomModel != null)
//{
// i = userCustomModel.CurrentPageIndex;
//}
userCustomModel = new UserCustom
{
user = db.Users.ToList(),
userDDL = db.Users.ToList()
};
// end load customer
var res = (from s in userCustomModel.user
select s);
res = res.ToList(); /// select all users from the database
if (userCustomModel.CurrentPageIndex == 0)
{
userCustomModel.CurrentPageIndex = 0;
}
userCustomModel.PageSize = 10;
userCustomModel.PageCount = ((res.Count() + userCustomModel.PageSize - 1) / userCustomModel.PageSize);
// (7 + 5 - 1) = 11 / 5 = 2 pages
if (userCustomModel.CurrentPageIndex > userCustomModel.PageCount)
{
userCustomModel.CurrentPageIndex = userCustomModel.PageCount;
}
userCustomModel.user = res.Skip(userCustomModel.CurrentPageIndex * userCustomModel.PageSize).Take(userCustomModel.PageSize);
// 0 * 5 = 0 (5)
// 1 * 5 = 5 skip 5.take 5-10
return View(userCustomModel);
}
【问题讨论】:
-
您还需要共享根据所选页码检索数据的代码。
-
CurrentPageIndex 如何填充和更改? CurrentPageIndex 的初始值是多少?
-
@ChetanRanpariya 我将如何以及在哪里得到它?通常在哪里?我很抱歉这个noobish问题,我正在努力解决。 :)
-
@user65439 CurrentPageIndex 的初始值为 0。每次循环进行时,它都会迭代 +1。这就是为什么到目前为止第 1 页和第 2 页存在的原因。
-
如果您想要更准确的答案,请分享索引操作方法的实现
标签: c# asp.net pagination