简体   繁体   English

MVC3中的WebGrid分页问题

[英]WebGrid pagination issue in MVC3

The issue is that pagination is working fine till the 9th page number and when I request the page which is in double or more digits then it takes the last digits and ignore the rest, say eg If I request a page number 10 then it displays the page 0 by passing "?page=0" as query string and discards the 1 and if I request a page number say eg 458 then it shows the page number 8 by passing "?page=8" as query string and discards the 45 from 458. Here is my code: 问题是,分页在第9页之前都可以正常工作,当我要求输入两位数或更多位数的页面时,它将使用最后一位数字,而忽略其余的数字, 例如例如,如果我要求页面编号10,则它将显示通过将“?page = 0”作为查询字符串传递给页面0并丢弃1,如果我请求一个页面编号,例如458,则通过将“?page = 8”作为查询字符串来显示页面编号8,并从458.这是我的代码:

In Javacsript: 在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>

In view: 鉴于:

    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)

and in Controller: 在控制器中:

    [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;
    }

Any help would be great! 任何帮助将是巨大的! If you still need to ask something more then please ask me. 如果您仍然需要问更多问题,请问我。 Thanks! 谢谢!

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

Correct: 正确:

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

You need that "+" inside the "()" because that "+" is required on the match pattern and not the pattern group for what you are trying to do here. 您需要在“()”中添加“ +”,因为匹配模式中需要“ +”,而不是您要在此处进行操作的模式组。

Regular expressions are always tricky!! 正则表达式总是很棘手!

The code that you have shared doesn't give enough of information to help troubleshoot this. 您共享的代码没有提供足够的信息来帮助解决此问题。 I think the problem might be somewhere in the code where you are submitting the request to load next page numbers in your web grid. 我认为问题可能出在您提交请求以在Web网格中加载下一页页码的代码中。 And as you have yourself mentioned: 正如您自己提到的:

* *

"say eg If I request a page number 10 then it displays the page 0 by passing "?page=0" as query string and discards the 1 " “例如,如果我请求页码为10,则它将通过传递“?page = 0”作为查询字符串来显示页面0,并丢弃1”

* *

I think the problem is really where your request/querystring gets formed! 我认为问题实际上是您的请求/查询字符串形成的地方! You may share that piece of code to help identify the issue. 您可以共享该段代码以帮助发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM