title: C# MVC 2 HtmlHelper to generate Table Columns with Model Binding
body: <p>I'm after an MVC2 custom htmlHelper that will allow me to dynamically create X amount of columns given the data.</p>
  
  <p>Say for instance I have a Zoo class which contains a list of Animals (which also contains a sub class). Ex:</p>
  
  <pre><code>public class Zoo
  {
    public List&lt;Animals&gt; myAnimals;
  }
  
  public class Animals 
  {
    public string year;
  
    public WarmClimate warm;
  
    public class WarmClimate {
      public string hippo;
      public string zebra;
      public string elephant;
      etc...
    }
  }
  </code></pre>
  
  <p>and I want to create a table similar to the following.  Essentially creating a  column for every list of Animals I have.  Could be 4 columns worth of data or could be 30.</p>
  
  <pre><code>   |             | 2011 | 2012 | 2013 | 2014 |
     | hypo        | 6    | 1    | 7    |  0   |
     | zebra       | 1    | 1    | 2    |  1   |
     | elephant    | 1    | 1    | 3    |  0   |
  </code></pre>
  
  <p>I currently have something like this; which is quite basic. Simply a for loop that iterates over the list grabbing the defined property value. If there were 100 animals in my class, a page with 100 for loops like this is a nightmare. </p>
  
  <pre><code>&lt;tr&gt;
      &lt;td&gt;Zebras: &lt;/td&gt;
      &lt;% 
          for (int i = 0; i &lt; Model.myAnimals.Count; i++)
          {
      %&gt;
          &lt;td&gt;&lt;%= Html.EditorFor(x =&gt; x.myAnimals[i].warm.zebra) %&gt;&lt;/td&gt;
      &lt;%
          }
      %&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>I Would like to replace that with a custom htmlhelper that would allow me to do something like:</p>
  
  <pre><code>&lt;tr&gt;
    &lt;td&gt;Zebras: &lt;/td&gt;
    &lt;%= Html.MyCustHelper( property?? , list??, expression??) %&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>MyCustHelper would return the appropriate html with NAME attribute populated correctly for model binding, much like it does when I use <code>Html.EditorFor()</code> helper, ex:</p>
  
  <pre><code>&lt;td&gt;&lt;input name="myAnimals[2].warm.zebra" id="myAnimals_2__warm_zebra" type="text" value="1"&gt;&lt;/td&gt; etc.... etc...
  </code></pre>
  
  <p>Maybe I'm going about this all wrong, but I assume there would be a simple way to output repeatable columns worth of data.</p>
  
  <p>Can anyone help out with an efficient way of doing this?</p>