MVC3中的WebGrid分页问题
本文关键字:问题 分页 WebGrid 中的 MVC3 | 更新日期: 2023-09-27 18:13:43
问题是,分页一直工作到第9个页码,当我请求两位数或两位数以上的页面时,它会使用最后一位数字,而忽略其余数字,例如,如果我请求一个页码10,它将通过传递"?page=0"作为查询字符串来显示页码0,并丢弃1;如果我请求页码,例如458,它将传递"?page=8"作为查询串来显示页码8,并丢弃458中的45。这是我的代码:
在Javascript中:
$(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;
}
任何帮助都会很棒!如果你还需要问更多的问题,请问我。谢谢
// 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];
您需要"(("中的"+",因为匹配模式中需要"+"而不是您在此处尝试执行的模式组。
正则表达式总是很棘手!!
您共享的代码没有提供足够的信息来帮助解决此问题。我认为问题可能出现在代码中,您正在提交在web网格中加载下一个页码的请求。正如你自己所提到的:
*
"例如,如果我请求一个页码10,则它显示页面0将"?page=0"作为查询字符串传递,并丢弃1">
*
我认为问题实际上在于你的请求/querystring是在哪里形成的!您可以共享这段代码来帮助识别问题。