Joseph Bulger IV

Consulting

Creating an PagedList<T> that uses AJAX

by Joseph on Apr.14, 2010, under Consulting, Programming, Technology

I’ve been using this PagedList functionality that i found from a blog article Rob Conery put up, and a control I found by Robert Muehsig which I’ve really enjoyed using so far.

One of the things that was missing from the functional set that I ended up needing was the ability to page the list, but through issuing AJAX requests instead of the typical post back.

So I went off and extended the existing model to support AJAX requests, and thought I would share it in case anyone else needed to do the same thing.

I guess the best place to start would be the use case.  So to start I created a control that encapsulates the Paging UI layout and calls I need.  The use of the original control looks like this:


Html.RenderPartial("AjaxPagination",
    new AjaxPaginationViewData
        {
            PageIndex = Model.PageIndex,
            Action = "CondoPage",
            Controller = "Home",
            AjaxOptions =
                new AjaxOptions { UpdateTargetId = "updatedContent" },
            TotalCount = Model.TotalCount,
            PageSize = Model.PageSize,
            NumberOfPagesToEachSide = 2
        }
);

The new AJAX functionality is called similarly:


<% using (Ajax.BeginForm("SomePage",
        "SomeController",
        new AjaxOptions { UpdateTargetId = "updatedContent" })) { %>

        <% Html.RenderPartial("AjaxPagination",
                new AjaxPaginationViewData {
                        PageIndex = Model.PageIndex,
                        Action = "SomeAction",
                        Controller = "SomeController",
                        AjaxOptions = new AjaxOptions
                                { UpdateTargetId = "updatedContent" },
                        TotalCount = Model.TotalCount,
                        PageSize = Model.PageSize,
                        NumberOfPagesToEachSide = 2
                });%>

<% } %>

A couple things to note. You’ll notice that the AJAX control is rendered inside a Ajax.BeginForm. This is because I’m using the Microsoft.Ajax way of making AJAX calls.  This could also be done using jQuery or something else that can process AJAX calls. I just went this way because the scripts are already included in asp.net mvc app when you first create the project.  The result of the AJAX call will be a partial view, and we’ll need to put that somewhere.  That’s where the UpdatedTargetId comes into play. Other things we include in the AJAX control that are not in the original are the Action and the Controller, and some AjaxOptions. PageActionLink doesn’t work with the AJAX control, because we’ll be using Ajax.ActionLink to build the link, which is why I broke it up into Action, and Controller. For the AjaxOptions, we need those to specify the target of the call.

So now that’s been explained, let’s look at the controls themselves.  Here’s a comparison of the original control and the ajax control.

The original is one this way:


<% if (Model.HasPreviousPage) { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", (Model.PageIndex - 1).ToString())%>">Previous</a>
<% } %>

<% if (Model.GetFirstPageToLink() != 1) { %>...<% } %>

<%for (var page = Model.GetFirstPageToLink(); page <= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %>
        <%=page.ToString()%>
<% } else { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>"><%=page.ToString()%></a>
<% } 

    if (page != Model.GetLastPageToLink()) { %>|<% } } %>

<% if (Model.GetLastPageToLink() != Model.PageCount) { %>...<% } %>

<% if (Model.HasNextPage) { %>
    <a href="<%=Model.PageActionLink.Replace("%7Bpage%7D", (Model.PageIndex + 1).ToString())%>">Next</a>
<% } %>

And the AJAX control is done this way:


<% if (Model.HasPreviousPage) { %>

<%= Ajax.ActionLink("Previous", Model.Action, Model.Controller, new { page = (Model.PageIndex - 1).ToString() }, Model.AjaxOptions)%>

<% } %>

<% if (Model.GetFirstPageToLink() != 1) { %>...<% } %>

<%for (var page = Model.GetFirstPageToLink(); page <= Model.GetLastPageToLink(); page++) {
    if (page == Model.PageIndex) { %>
        <%=page.ToString()%>
    <% } else { %>

<%= Ajax.ActionLink(page.ToString(), Model.Action, Model.Controller, new { page = page.ToString() }, Model.AjaxOptions)%>

<% } if (page != Model.GetLastPageToLink()) { %> | <% } } %>

<% if (Model.GetLastPageToLink() != Model.PageCount) { %>...<% } %>

<% if (Model.HasNextPage) { %>

<%= Ajax.ActionLink("Next", Model.Action, Model.Controller, new { page = (Model.PageIndex + 1).ToString() }, Model.AjaxOptions)%>

<% } %>

The big difference here is the way that the links are generated. The original control simply creates an anchor tag and passes in the url generated by the Model. The AJAX control uses AJAX.ActionLink() instead, so we can have the link support AJAX.

So knowing how the control looks, this is the Model for the AJAX control itself:


public class AjaxPaginationViewData
{
    public int NumberOfPagesToEachSide { get; set; }
    public int PageIndex { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }

    public string Action { get; set; }
    public string Controller { get; set; }

    public AjaxOptions AjaxOptions { get; set; }

    public int PageCount
    {
        get
        {
            return (int)Math.Ceiling((double)TotalCount / PageSize);
        }
    }
    public bool HasPreviousPage
    {
        get
        {
            return (PageIndex > 1);
        }
    }

    public bool HasNextPage
    {
        get
        {
            return (PageIndex * PageSize) <= TotalCount;
        }
    }

    public int GetFirstPageToLink()
    {
        return (PageIndex - NumberOfPagesToEachSide > 1 ? PageIndex - NumberOfPagesToEachSide : 1);
    }

    public int GetLastPageToLink()
    {
        return (PageIndex + NumberOfPagesToEachSide < PageCount ? PageIndex + NumberOfPagesToEachSide : PageCount);
    }
}

That pretty much explains how the control is built.

The only thing left is how the interaction with PagedList happens.  For that we look at the action that the control calls.  In this example, we’re calling SomeAction in SomeController, and it would look something like this:


public ActionResult SomeAction(int page)
{
    CachedPage = page;
    var query = GetSearchQuery(CachedSearchParameters);
    var model = query.ToPagedList(page, DefaultPageSize);
    return PartialView("AjaxResults", model);
}

The ToPagedList performs the functionality that is included with the PagedList classes which you can find here.

Let me know what you think, and if you’d like some demo source to see this in action I can happily provide, just let me know.

Leave a Comment :, , , more...

Concept for Business Card

by Joseph on Jan.05, 2010, under Consulting

Is this hard to understand?

What is the email address on this card?

businesscardmockup

Maybe this is more understandable?

businesscardmockup2

or I could swap the email and the site, maybe that makes it easier to read.

businesscardmockup3

I’m getting there! This was suggested by Steven, and I think he’s completely correct.

businesscardmockup4

Leave a Comment : more...

Building a Reputation System

by Joseph on Sep.03, 2009, under Church, Consulting, Programming, Technology

I’m working on a reputation system for a site I’ve been recently working on (http://www.serveandtrade.com).  I’m going through some ideas so I thought I’d post them on here and see what people think.

Let’s start buy throwing up what we currently have so we can see where we’re trying to go.  Here’s a mockup of what a user will see when they search for trades right now.

currentsearchconcept

I have a couple problems with how it works right now.

  1. I don’t like that it’s in a grid/table format.  I have to fix that first.  I’m moving more towards something like a list layout.
  2. There are some things missing that I would like to be able to do.  For instance, if I just searched for “can of soup”, and I see that Michelle has the one I want, I would like to have a button/link/something to click on that says “I want that!”  Right now the only thing you can do is go look at the trade, or ask a question.
  3. Clicking on the owner’s link takes you to their profile, but it doesn’t show you what other trades they have, or trades they are looking for.  That information could really be useful.

I’m sure there are other things I could think of, but for now I’m going to start focusing on these three and build some mockups to illustrate these workflows.

First, getting rid of the grid.

newsearchconcept

This search list looks a lot better I think.  There are some more features on here then the other one, but we’ll go over those in subsequent posts.

So at this point I’m looking for any feedback.  ANY feedback, good or bad… I’ll take it all.  I really haven’t made up my mind at this point yet, but I think I’m heading in the right direction.

Next post will be about the “I want it!” feature.  Stay tuned!

Leave a Comment :, , more...

Client just went live

by Joseph on Mar.19, 2009, under Consulting

Hey guys, just had a client go live.  Interested in buying a diamond or engagement ring for your lady?  Check out http://www.edwardjamesandco.com.  Edward not only sells a large arrangement of engagement rings, but he has a huge selection of loose diamonds.

On a personal note, I’ve been working with Edward for the past 6 or so months, and he’s a really great guy.  The next time I buy my wife something of this caliber I’m definitely going to be going to him.

Even if you’re not in the market right now, go check out the site anyway.  He has a wealth of knowledge about diamonds and rings: what to look for in a diamond or setting, and other information about how the entire certification process works.  He’ll be updating his site regularly to keep people informed of his industry, so if you’re into diamonds you might want to consider registering on his site just for informational reasons.

Anyway, that’s my two cents, take care guys!

Leave a Comment :, , more...

  • Link with me

    Joseph Bulger
  • Call me

  • What I'm Doing...

    Powered by Twitter Tools

  • Looking for something?

    Use the form below to search the site:

    Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

    Visit our friends!

    A few highly recommended friends...

    Archives

    All entries, chronologically...