title: Best way to get table header names in MVC 4
body: <p>I'm trying to display a list of objects in a table.  I can iterate over each individual item to find it's value (using an for loop or a <a href="https://stackoverflow.com/a/11261629/875708">DisplayTemplate</a>), but how do I abitriarily pick one to display headers for the whole group.</p>
  
  <p>Here's an simplified example:</p>
  
  <p><strong>Model</strong>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ClientViewModel
  {
      public int Id { get; set; }
      public List&lt;ClientDetail&gt; Details { get; set; }
  }
  public class ClientDetail
  {
      [Display(Name="Client Number")]
      public int ClientNumber { get; set; }
      [Display(Name = "Client Forname")]
      public string Forname { get; set; }
      [Display(Name = "Client Surname")]
      public string Surname { get; set; }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>@model MyApp.ViewModel.ClientViewModel
  
  @{ var dummyDetail = Model.Details.FirstOrDefault(); }
  
  &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;@Html.DisplayNameFor(model =&gt; dummyDetail.ClientNumber)&lt;/th&gt;
              &lt;th&gt;@Html.DisplayNameFor(model =&gt; dummyDetail.Forname)&lt;/th&gt;
              &lt;th&gt;@Html.DisplayNameFor(model =&gt; dummyDetail.Surname)&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @for (int i = 0; i &lt; Model.Details.Count; i++)
          {
                &lt;tr&gt;
                  &lt;td&gt;@Html.DisplayFor(model =&gt; model.Details[i].ClientNumber)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(model =&gt; model.Details[i].Forname)&lt;/td&gt;
                  &lt;td&gt;@Html.DisplayFor(model =&gt; model.Details[i].Surname)&lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p><em>Notice</em>: I'm using <code>var dummyDetail = Model.Details.FirstOrDefault();</code> to get a single item whose properties I can access in <code>DisplayNameFor</code>.</p>
  
  <ul>
  <li>What would be the best way to access those headers ?</li>
  <li>Will this break if the collection is null?</li>
  <li>Should I just replace them with hard coded plain text labels?</li>
  </ul>