ASP.NET MVC ForEach-Loop Sugar

by paul on 8/26/2010 3:15:35 PM

When you make the move from classic ASP.NET MVC webforms, one of the funny questions you hear is, “Where is the repeater control?”. The MVC veteran responds, “It’s called a For-Loop”. However, if you are used to using the ASP.NET DataList control for doing things like providing repeating columns, then a standard ForEach-Loop won’t do without some help. While I’m sure there are a number of ways to handle this, I want to demonstrate a simple re-usable way to extend what information is available in the foreach loop.

To provide additional information to the loop, I’ve created an extension method that takes an IEnumerable<T> and returns a list of IEnumerable<ForEachItem<T>>. The extra data for the loop is provided in the ForEachItem<T> class.

public class ForEachItem<T> { public T Item { get; set; } public bool First { get; set; } public bool Last { get; set; } public int Index { get; set; } public bool NewGroup { get; set; } }

Here is a list of what each Property represents:

  • Item: This provides access to the original data item
  • First: This boolean is true if it is the first item in the list
  • Last: This boolean is true if it is the last item in the list
  • Index: This is the index of the item in the list
  • NewGroup: This bool represents if this item is the first in a new group (explained below)

The extension method simply iterates through the source list and creates a ForEachItem instance for each item.

public static IEnumerable<ForEachItem<T>> ToFor<T>(this IEnumerable<T> items) { return ToFor(items, 0); } public static IEnumerable<ForEachItem<T>> ToFor<T>(this IEnumerable<T> items, int group) { var list = new List<ForEachItem<T>>(); foreach (var item in items) { var fei = new ForEachItem<T> { First = (list.Count == 0), Item = item, Index = list.Count }; fei.NewGroup = (group > 0 && (list.Count % group == group - 1)); list.Add(fei); } if (list.Count > 0) list[list.Count - 1].Last = true; return list; }

The first extension method ToFor(items) can be used when you don’t require grouping. The second method allows you to provide an integer which will set NewGroup to true for every X items. This can be used to produce things like the RepeatColumns function of the DataList. Here is some example usage to create a table from a loop that has 4 columns across:

 

<table> <tr> <% foreach (var m in users.ToFor(4)) { %> <td><%= m.Item.Name %></td> <% if (m.NewGroup) app.Write("</tr><tr>"); %> <% } %> </tr> </table>

Another use could be to format a menu of links for a website that need to have a pipe character between the links:

<% foreach (var p in pages.ToFor()) { app.Write("<a href='{0}'>{1}</a>", p.Item.Href, p.Item.Title); if (!p.Last) app.Write("&nbsp;&nbsp;|&nbsp;&nbsp;"); } %>

If you find this useful, feel free to leave a comment and let me know. I’d also love to hear if you have other techniques that you use to solve the same sorts of problems.

Play Lord of the Rings Online Free September 10 - PCWorld

by paul on 8/18/2010 12:20:05 PM

Lord of the Rings Online

If you'd like to play the best online multiplayer game going for absolutely nothing, Lord of the Rings Online will be available, gratis, from September 10 forward.

Posted via email from paulwheeler's posterous

SlimPDF Reader

by paul on 8/3/2010 8:32:20 PM

Want a free, lightweight PDF reader to replace Adobe Reader?

  • Eliminate bloatware and help your computer run faster
  • View any PDF File just like Adobe Reader
  • 100% Free
  • The smallest desktop PDF Reader in the World.
  • SlimPDF Reader

    Game Table Online

    by paul on 8/3/2010 8:29:29 PM

    GameTable Online is an Internet game site featuring web versions of popular, real-world tabletop games that you play against other human players from around the world. Instead of scheduling a face-to-face game session, you can log on to GTO from a computer and play whenever you want.

    GameTable Online focuses on turn-based strategy games. All of our games are based on real-world games and are fully licensed (where applicable) so they look and play like their real-world counterparts. Our aim is to provide a variety of games from a variety of publishers.

    If there is a particular game that you would like to see on GTO, please let us know by posting your request in the Game Request forum board and tell the publisher of the game how their game would be a perfect at GameTable Online! If you are a game publisher and are interested in having your games brought to the Web, please contact Robert Eng.

    Posted via email from paulwheeler's posterous

    BatteryBar, the most accurate battery meter for Windows (free version available)

    by paul on 8/3/2010 8:29:56 PM

    If you are looking for a great battery bar for the taskbar in Windows, check this one out.

    Posted via email from paulwheeler's posterous


    First Previous 1 2 3 4 5  ... Next Last