<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1542</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1542" rel="self"/>
  <title>czemuja.myopenid.com</title>
  <updated>Mon Jan 25 23:55:23 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1158</id>
    <published>2010-01-25T23:55:23-08:00</published>
    <updated>2012-01-12T16:21:59-08:00</updated>
    <title>[C#] ASP.Net MVC HTML List helper</title>
    <content type="html">&lt;p&gt;I wanted to display list from my object:
&lt;br /&gt;user[x].Group[y].Name
&lt;br /&gt;I got some of code from here: &lt;a href="http://blog.wekeroad.com/blog/asp-net-mvc-list-helper-extension-method/" target="_blank"&gt;http://blog.wekeroad.com/blog/asp-net-mvc-list-helper-extension-method/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm wondering if there is a better way.&lt;/p&gt;

&lt;pre&gt;public static string ToFormattedList&amp;lt;T&amp;gt;(this HtmlHelper helper, IList&amp;lt;T&amp;gt; objectList, ListType listType, Func&amp;lt;T, string&amp;gt; valueReturn)
{

    string outerListFormat = &amp;quot;&amp;quot;;
    string listFormat = &amp;quot;&amp;quot;;

    switch (listType)
    {
        case ListType.Ordered:
            outerListFormat = &amp;quot;&amp;lt;ol&amp;gt;{0}&amp;lt;/ol&amp;gt;&amp;quot;;
            listFormat = &amp;quot;&amp;lt;li&amp;gt;{0}&amp;lt;/li&amp;gt;&amp;quot;;
            break;
        case ListType.Unordered:
            outerListFormat = &amp;quot;&amp;lt;ul&amp;gt;{0}&amp;lt;/ul&amp;gt;&amp;quot;;
            listFormat = &amp;quot;&amp;lt;li&amp;gt;{0}&amp;lt;/li&amp;gt;&amp;quot;;
            break;
        case ListType.TableCell:
            outerListFormat = &amp;quot;{0}&amp;quot;;
            listFormat = &amp;quot;&amp;lt;td&amp;gt;{0}&amp;lt;/td&amp;gt;&amp;quot;;
            break;
        default:
            break;
    }

    var sb = new StringBuilder();
    objectList.ForEach(item =&amp;gt; sb.AppendFormat(listFormat, valueReturn(item)));
    return string.Format(outerListFormat, sb);
}

public enum ListType
{
    Ordered,
    Unordered,
    TableCell
}

## Usage:
Html.ToFormattedList(item.Groups, UiHelpers.ListType.Unordered, g =&amp;gt; g.GroupName )&lt;/pre&gt;</content>
    <author>
      <name>czemuja.myopenid.com</name>
      <email>czemuja@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1158-asp-net-mvc-html-list-helper" rel="alternate"/>
  </entry>
</feed>

