使用 Ajax 和 MvcContrib Grid 添加分页和排序功能

发布于 2024-11-28 14:33:17 字数 1776 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-12-05 14:33:17

如果其他人遇到同样的问题,请发送附加参数。我更改了我的 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());
        }

现在我按如下方式调用该函数:

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. 

谢谢,

In case anybody else is having the same issue, with sending in additional parameters. I changed my html helper function as follows:-

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());
        }

And I now call the function as follows:

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. 

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文