【问题标题】:WebGrid pagination issue in MVC3MVC3 中的 WebGrid 分页问题
【发布时间】:2013-05-09 17:50:05
【问题描述】:

问题是分页在第 9 页码之前工作正常,当我请求两位数或更多位数的页面时,它会占用最后一个数字并忽略其余数字,比如说如果我请求页码 10,那么它通过将“?page = 0”作为查询字符串传递并丢弃 1 来显示第 0 页,如果我请求页码,例如458 然后它通过传递 "?page=8" 作为查询字符串来显示页码 8,并丢弃 458 中的 45。 这是我的代码:

在 Javacsript 中:

        $(function () {
    $('tfoot a').click(function () {
        // try to extract the page number from the link
        var page = this.href.match(/page=([0-9])+/)[1];

        // submit the form so that the POST action is invoked
        var form = document.forms[0];
        form.action = '/Session/Index?page=' + page;
        form.submit();

        return false;
    });
});
      </script>

鉴于:

    var grid = new WebGrid(source: Model.ListSessions, rowsPerPage: 5,
    canPage: true, canSort: false, defaultSort: "Absentee");

     @grid.GetHtml(
    htmlAttributes: new { id = "grid" },
    tableStyle: "grid",
    headerStyle: "head",
    alternatingRowStyle: "alt",
    columns: grid.Columns(
                            grid.Column(format: @<text> <input type="hidden" id="ColorCodeValue" name="ColorCodeValue" value="@item.ColorCodeValue" /> </text>, style: "width:0px;"),
                            grid.Column("CreatedBy", "Created By"),
                            grid.Column("Session Title", format: (item) => Html.ActionLink((string)item.SessionTitle, MVC.Session.ActionNames.EditSession,
                            new { id = item.SessionId })),
                            grid.Column("SessionID", "Simple Session ID"),
                            grid.Column("StartDate", "Start Date"),
                            grid.Column("StartTime", "Start Time"),                               
                            grid.Column("SessionStatus", "Status"),
                            grid.Column("Prep Materials", format: @<text><a  onclick="SessionClick();" id="@item.EnableViewLink" href="@Url.Action(MVC.Session.Actions.ViewSession((string)item.SessionID))">View</a><a id="UploadSessionMaterial"  href="@Url.Action(MVC.Session.Actions.UploadSession((Int32)item.SessionId))">Upload</a>  </text>),
                            grid.Column("Action", format: @<text><a   href="@Url.Action(MVC.Session.Actions.ViewSession((int)item.SessionId))">View</a><a id="@item.IsPublished" class="@item.IsTranscriptExists" href="@Url.Action(MVC.Session.Actions.EditTranscript((Int32)item.SessionId))">Edited Session </a></text>)
                                     ), mode: WebGridPagerModes.All)

在控制器中:

    [HttpPost]
    public virtual ActionResult Index(SessionViewModel model)
    {
        model = GetSessionListing(model);
        return View(model);
    }


    private SessionViewModel GetSessionListing(SessionViewModel model)
    {
        if (model == null)
        {
            model = new SessionViewModel();
        }
        int page = 1;
        if (Convert.ToInt64( Request["page"]) != null)
            int.TryParse(Request["page"], out page);

        //Rest of Coding here           

        return model;
    }

任何帮助都会很棒! 如果您仍然需要询问更多内容,请询问我。 谢谢!

【问题讨论】:

    标签: c# asp.net asp.net-mvc-3 paging webgrid


    【解决方案1】:

    您共享的代码没有提供足够的信息来帮助解决此问题。我认为问题可能出在您提交请求以在 Web 网格中加载下一页编号的代码中。正如你自己提到的:

    *

    “例如,如果我请求页码 10,那么它会显示第 0 页 将 "?page=0" 作为查询字符串传递并丢弃 1 "

    *

    我认为问题出在您的请求/查询字符串的形成位置!您可以分享这段代码以帮助识别问题。

    【讨论】:

    • 您好@Nagesh Chopra,感谢您的回复,请再次查看我的代码我已经更新了它,因为我忘记发布代码我正在请求页面。
    【解决方案2】:
    // try to extract the page number from the link
    var page = this.href.match(/page=([0-9])+/)[1];
    

    正确:

    // try to extract the page number from the link
    var page = this.href.match(/page=([0-9]+)/)[1];
    

    您需要在“()”中使用“+”,因为匹配模式需要“+”,而不是您在此处尝试执行的模式组。

    正则表达式总是很棘手!!

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 2011-09-14
      • 1970-01-01
      • 2011-07-20
      • 2012-12-06
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多