title: C# MVC4 Razor - can DisplayNameFor be used to get a Display(Name = ...) of a different model?
body: <p>When I have a <em>Model</em> in my <em>View</em>, I can easily access a member's <em>DisplayName</em> by</p>
  
  <pre><code>@Html.DisplayNameFor(m =&gt; m.myMember)
  </code></pre>
  
  <p>But when I need the <em>DisplayName</em> of a member from a different model there seems to be no easy solution, there are some that are digging in the <em>ModelMetaData</em> or <em>System.ComponentModel.DataAnnotations.DisplayAttribute</em>, but these are all some multistep things that are not easy to read or understand.</p>
  
  <p>If you take a look at the definition of <code>DisplayNameFor</code></p>
  
  <pre><code>DisplayNameFor&lt;TModel, TValue&gt;(
      this HtmlHelper&lt;IEnumerable&lt;TModel&gt;&gt; html,
      Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression
  )
  </code></pre>
  
  <p>It seems that it could be easily used by something like</p>
  
  <pre><code>@Html.DisplayNameFor&lt;MyDifferentModel, string&gt;(?, m =&gt; m.memberInMyDifferentModel)
  </code></pre>
  
  <p>but I don't get the clue about one of the parameters <em>TModel</em> is the model, <em>TValue</em> the type of my member, <em>Expression...</em> the <code>m =&gt; m.myMember</code> thingy, but what's that other parameter? Is it possible to use <code>DisplayNameFor</code> in that way? And what are the missing pieces?</p>
  
  <p>Thanks for any suggestions - the examples in the www and on SO all handle just the case where you have the model in the view, nested models in the view, whatever what you can access directly by the model in the view, but that's not what I want.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>At the moment I use this helper function to get the display name</p>
  
  <pre><code>public string getDisplayName&lt;Type&gt;(string member)
  {
      var type = typeof(Type);
      var memInfo = type.GetMember(member);
      var attributes = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);
      var displayname = ((System.ComponentModel.DataAnnotations.DisplayAttribute)attributes[0]).Name;
          return displayname;
  }
  </code></pre>
  
  <p>Can be easily used with</p>
  
  <pre><code>getDisplayName&lt;MyDifferentModel&gt;("memberInMyDifferentModel");
  </code></pre>
  
  <p>I just wondered if this functionality is already built in the HTML helper <code>DisplayNameFor</code> ...</p>