使用Ajax和MvcContrib网格添加分页和排序功能
我正在MVC应用程序中使用MvcContrib网格控件。按照Pro*ASP.NET MVC手册中的示例,我创建了以下类和分页助手 公共类分页信息使用Ajax和MvcContrib网格添加分页和排序功能,ajax,sorting,paging,mvccontrib,Ajax,Sorting,Paging,Mvccontrib,我正在MVC应用程序中使用MvcContrib网格控件。按照Pro*ASP.NET MVC手册中的示例,我创建了以下类和分页助手 公共类分页信息 { public int TotalItems { get; set; } public int ItemsPerPage { get; set; } public int CurrentPage { get; set; } public int TotalPages { get { return
{
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());
}
}
(一)
如何使用Url.Action将pageSize(即行数)发送到helper类
(二)
我还定义了以下函数来启用ajax。这是一种有效的方法吗?我也打算对soring使用类似的函数。因此,您的意见将不胜感激
$(“#页面链接a”).live(“单击”,函数(){
谢谢,如果其他人也有同样的问题,请发送额外的参数。我更改了我的html帮助函数,如下所示:-
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, 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, pagingInfo.ItemsPerPage));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
谢谢,如果你的答案解决了你的问题,你应该接受它。
$.get($(this).attr("href"), function(response) {
$("#Grid").replaceWith(response);
});
return false;
});
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, 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, pagingInfo.ItemsPerPage));
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, j) => Url.Action("Index", new{pageNo = i, pageSize= j}))
It works a treat now. Must RTFM more. Thanks to the Plural Sight videos for helping me resolve this one.