【问题标题】:Custom pagination, "next" and "last" button doesn't work properly自定义分页、“下一个”和“最后一个”按钮无法正常工作
【发布时间】: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


【解决方案1】:

显示给用户的数字和索引之间存在差异。索引值在0 .. PageCount-1 范围内,显示的数字在1 .. PageCount 范围内。

现在,如果我们查看 NextLast 链接,我们可以看到两种方法混合在一起:

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

第一个链接增加CurrentPageIndex,这是一种有效的方法。然而,第二个将索引设置为PageCount,而索引范围为我们建立的0 .. PageCount-1。这或许可以解释为什么 Last 链接不起作用但 Next 链接在这种情况下应该起作用。

还有支票:

@if (Model.CurrentPageIndex < Model.PageCount)

永远是真的,因为CurrentPageIndex 最多可能是PageCount-1

我建议为您的 Index 操作方法设置一个断点并尝试各个链接以查看您获得的值以及它们是否符合您的期望。虽然Next链接不起作用的原因尚不清楚,但应该可以根据描述从代码中找出。

“下一步”按钮只返回当前页面,或者如果我在另一个页面 大于 1 页,则返回 1 页。

从这种行为中,我怀疑代码隐藏处理会从 page 参数中减去 1,因此 URL 需要 1 .. PageCount 范围内的值,但您提供 0 .. PageCount-1 范围内的值。

更新

在您将操作方法​​代码添加到您的问题后(​​Controller 中的每个公共方法在 ASP.NET 中按约定称为action method :-)),我正在用我的发现更新我的代码。

首先您的代码包含以下检查:

if (userCustomModel.CurrentPageIndex == 0)
{
   userCustomModel.CurrentPageIndex = 0;
}

你可以摆脱它,因为它不会改变任何东西:-)。

然而,主要问题在这里:

userCustomModel = new UserCustom
{
    user = db.Users.ToList(),
    userDDL = db.Users.ToList()
};

在这里,您将userCustomModel 设置为UserCustom 的新实例,这实际上覆盖了您从请求URL 收到的值。如果您仅在null 时初始化变量,并在下一行直接从数据库中查询,您的代码应该可以工作:

public ActionResult Index(UserCustom userCustomModel = null)
{
    //initialize only if null
    if ( userCustomModel == null )
    {
       userCustomModel = new UserCustom();
    }
    // end load customer

    var res = db.Users.ToList(); // select all users from the database

    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

    userCustomMode.userDDL = res;


    return View(userCustomModel);
}

如果这不起作用,请检查路由,如果您的路由参数实际上称为CurrentPageIndex。您的操作方法参数可以简化为只使用一个int

public ActionResult Index(int currentPageIndex = 0)

现在您的代码可以在每次使用currentPageIndex 变量作为当前页面时初始化userCustomModel

【讨论】:

  • 非常感谢马丁!但是,请稍等,我现在正在调试我的代码并按照您的说明进行操作..
  • 两天后,我终于设法让它在没有 EF 等的情况下工作。非常感谢您的大力帮助!我将尝试发布我的小调整以及我如何使其完全正常工作,但这对我帮助很大。非常感谢!
【解决方案2】:

伙计,我不知道您为什么需要通过提交表单来处理导航。

请备注:

//$(".pager").click(function (evt) {
//    var pageindex = $(evt.target).data("pageindex");
//    $("#CurrentPageIndex").val(pageindex);
//    evt.preventDefault();
//    $("form").submit();
//});

然后再试一次。

我希望它有所帮助,因为我在没有使用上面更新的控制器的情况下进行了尝试。

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 2014-07-13
    • 2021-10-21
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多