ajaxsortingpaginationmvccontrib

Adding Paging and Sorting functionality using Ajax and MvcContrib Grid


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,


Solution

  • 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,