Tag: linkedIn
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.
Rethinking the Reputation System
by Joseph on Sep.07, 2009, under General
Just FYI, my wife is very wise. I had her look over the design I came up for the reputation system. She thought it would be easier if the user could just check off the things they wanted.
Sometimes its just better to get a second opinion!
This is the new concept. Not much has changed visually, but the behavior will definitely be different.
So the behavior will be by checking off one of the items from the search you’ll be informing the owner that you’re interested in that item. What happens after that point I’m still working out, but that basically wraps up the behavior for this side. What we have left to do is get the behavior done for the owner’s side.
Now my problem is I need to design a checkbox that will go on the side so people can just to click on.
So far I’ve come up with this, but I’m not 100% satisfied with it. The coloring won’t really get nailed down until we put it up on the site and tweak it to make the color scheme, but you get the idea.
Any suggestions?
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.
I have a couple problems with how it works right now.
- 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.
- 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.
- 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.
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!
The Mock that Rocks
by Joseph on Jul.02, 2009, under Technology
I’ve discovered a product that I really enjoy using and thought I would share it. It’s called Balsamiq Mockups and it’s a tool that you can use online or on your computer for drawing up quick ideas on software systems. They use a sketchy kind of font to give you that “drawing out a spec” feel. What really makes the product so appealing is how fast you can build up a simple mock to show to your team. I’m literally able to sit down at my computer and within 5 minutes have a meaningful mockup of some kind of feature I want to convey. Really powerful stuff.
Here’s an example of the kind of thing you can build with it.
C++ had it right, Multiple Inheritance Rocks, Single Inheritance… not so much.
by Joseph on Jun.03, 2009, under Programming, Technology
I’ve been having a lot of problems with my coding lately. I primarily program in C#, but I originally came from a C++ world. When I first learned Java, and was introduced to the idea of Single Inheritance, the sales pitch made a lot of sense to me. How can any class really derive from multiple classes? I drank the kool-aid, and I’ve been living in a Single Inheritance world ever since.
Now, the caveat to all of this was that you had this thing called an interface that actually allowed you to define declarations of behavior that could later be implemented. This could be done any number of times. This also made a lot of sense, and they would do some simplistic model that illustrates the concept. I imagine for purposes of this article we should probably illustrate something as well just to drive home the point. Let’s say you have a Coin. A Coin is a kind of Currency. So the simple inheritance chain would dictate that Coin inherits from Currency, and everything is fine. Of course, in order for this to matter at all, there needs to be some benefit to having Coin inherit from Currency. So let’s say that Currency has some behavior called CalculateBuyingPower that actually figures out what kind of buying power that particular Currency has. Ok, so now we have an inheritance chain, and we have a direct benefit, figuring out the buying power of the Coin.
If that’s as far as you take it, then everything’s all fine and dandy, and Single Inheritance is great. The problem is, that in the Real World, what we call Objects can legitimately “be” multiple things. This is equally true in the Software World. So, going back to the Coin, not only is the Coin a Currency, but the Coin is also a MetalConstruct. A MetalConstruct is just my way of saying something that is made of metal. Why do I care? Simple. At some point during the life of a Coin, it is entirely possible that the buying power of the Coin will reach a point that it would actually be better to melt down the Coin because the value of the Metal in the coin is much higher (look at what a penny is made of nowadays and you’ll see what I mean). Now, a lot of you are going to say, “That’s fine, just define a MetalConstruct interface and you should be good to go.” In a lot of cases that might be true, but in this example we fine a very big problem. The point of the MetalConstruct is to normalize the behavior of melting down the Coin. No matter what kind of MetalConstruct you have, the melting process for it is always going to be the same. Sure there are variations in melting temperature and what not, but the overall process is the same. The point being, that an interface can only declare that behavior MeltDown should exist, but not define the implementation of it. This is the problem with Single Inheritance. I now need a way for the Coin to be both a Currency and MetalConstruct, which provides the Coin with behaviors CalculateBuyingPower and MeltDown. Coin shouldn’t have to define how to do either of those behaviors, because that’s not it’s responsibility.
During software development, when you’re working on defining your domain, this can come up quite often, actually. With the advent of extension methods in .NET, you’re able to ease the pain a bit in C#, but the fundamental problem is really still there. What I would like to see would be one of two things. It would be nice if you could inherit from multiple abstract classes, which would allow you to define Contracts at the abstract class level. Another idea would be to bring forward the construct of a contract as a first-class citizen, it would be similar to an abstract class and an interface in the sense that you could not create one directly, but would allow for definition of behavior while still being able to have a class implement numerous contracts. It would be a totally new way of defining an object, apart from class, struct, or interface entirely perhaps.
So now I’m looking back at C++ and I’m thinking it would be really nice if I had Multiple Inheritance so I can just get past this ridiculous wall I’m having to climb. Possibly with .NET 4.0, Microsoft is bringing forth the idea of Code Contracts , which might help when you’re trying to get around Single Inheritance issues when dealing with Design by Contract, but for real domain model issues, I think the problem still remains.
Food for thought.


