title: Using DisplayNameFor and DisplayFor whilst looping list within model
body: <p>I have created a ViewModel which I am using as the model for my view. Within the view model it contains the following definition:</p>
  
  <pre><code>public class ItemDetailViewModel
  {
      public int ItemId { get; set; }
      public bool Active { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public List&lt;ItemDetailItemOptionViewModel&gt; ItemOptions { get; set; }
  }
  </code></pre>
  
  <p>As you can see it contains a List&lt;> named ItemOptions. My view model is tied to ItemDetailViewModel. Within the view I loop through each element in the list and output them:</p>
  
  <pre><code>@model app1.Models.ViewModels.ItemDetailViewModel
  
  @{
      ViewBag.Title = "Item " + @Html.DisplayFor(model =&gt; model.ItemId);
  }
  
  //...
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Active&lt;/th&gt;
          &lt;th&gt;Names&lt;/th&gt;
      &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
      @foreach (app1.Models.ViewModels.ItemDetailItemOptionViewModel ItemOption in Model.ItemOptions)
      {
          &lt;tr&gt;
              &lt;td&gt;@ItemOption.Active&lt;/td&gt;
              &lt;td&gt;@ItemOption.Name&lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/tbody&gt;
  //...
  </code></pre>
  
  <p>What I want to be able to do is instead of hard coding the name and values, I want to be able to user the helpers I use else where within the page, such as:</p>
  
  <pre><code>@Html.DisplayNameFor(model =&gt; model.ItemCode)
  @Html.DisplayFor(model =&gt; model.ItemCode)
  </code></pre>
  
  <p>I've tried the following </p>
  
  <pre><code>@Html.DisplayNameFor(model =&gt; model.ItemOptions.ItemCode)
  @Html.DisplayFor(model =&gt; model.ItemOptions.ItemCode)
  </code></pre>
  
  <p>But this gives the following error:</p>
  
  <blockquote>
    <p>Error 1   'System.Collections.Generic.List&lt;[].Models.ViewModels.ItemDetailItemOptionViewModel>'
    does not contain a definition for 'ItemCode' and no extension method
    'ItemCode' accepting a first argument of type
    'System.Collections.Generic.List&lt;[].Models.ViewModels.ItemDetailItemOptionViewModel>'
    could be found (are you missing a using directive or an assembly
    reference?)   ....\Views\Item\Details.cshtml  87  81  []</p>
  </blockquote>
  
  <p>I've also tried doing it off the ItemOption object which I loop on but get similar issues. What am I doing wrong?</p>