使用 Ajax 和 MvcContrib Grid 添加分页和排序功能
我在 MVC 应用程序中使用 MvcContrib 网格控件。按照 Pro*ASP.NET MVC 书中的示例,我创建了以下类和分页助手
公共类 PagingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
在我的视图中,我有以下内容:
Page: @Html.PageLinks( ViewBag.Paging as PagingInfo, i => Url.Action("Index", new{pageNo = i})) @Html.DropDownList("pageSize", new SelectList(new[] { "2", "25", "50", "75", "100", "200" }), "-Select-", new { @style = "width:75px" })
1) 如何使用 Url.Action 将 pageSize(即行数)发送到帮助程序类?
2) 我还定义了以下函数来启用 ajax。这是一种有效的方法吗?我也打算使用类似的函数来进行排序。所以您的意见将不胜感激。
$("#pageLinks a").live("click", function() {
$.get($(this).attr("href"), function(response) {
$("#Grid").replaceWith(response);
});
return false;
});
谢谢,
I am using MvcContrib Grid control in my MVC application. Following the example in the Pro*ASP.NET MVC book I have the following class and paging helper created
public class PagingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
In my View, I have the following:
Page: @Html.PageLinks( ViewBag.Paging as PagingInfo, i => Url.Action("Index", new{pageNo = i}))
@Html.DropDownList("pageSize", new SelectList(new[] { "2", "25", "50", "75", "100", "200" }), "-Select-", new { @style = "width:75px" })
1)
How do I send in the pageSize (which is the number of rows) to the helper class using the Url.Action?
2)
I have also defined the following function to enable ajax. Is this an efficient way to go about it? I was going to use a similar function for the soring too. So your views would be appreciated.
$("#pageLinks a").live("click", function() {
$.get($(this).attr("href"), function(response) {
$("#Grid").replaceWith(response);
});
return false;
});
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他人遇到同样的问题,请发送附加参数。我更改了我的 html 帮助器函数,如下所示:-
现在我按如下方式调用该函数:
谢谢,
In case anybody else is having the same issue, with sending in additional parameters. I changed my html helper function as follows:-
And I now call the function as follows:
Thanks,