title: Wondering why DisplayName attribute is ignored in LabelFor on an overridden property
body: <p>today I got confused when doing a couple of <code>&lt;%=Html.LabelFor(m=&gt;m.MyProperty)%&gt;</code> in ASP.NET MVC 2 and using the <code>[DisplayName("Show this instead of MyProperty")]</code> attribute from <code>System.ComponentModel</code>.</p>
  
  <p>As it turned out, when I put the attribute on an overridden property, LabelFor didn't seem to notice it.<br>
  However, the <code>[Required]</code> attribute works fine on the overridden property, and the generated errormessage actually uses the DisplayNameAttribute.</p>
  
  <p>This is some trivial examplecode, the more realistic scenario is that I have a databasemodel separate from the viewmodel, but for convenience, I'd like to inherit from the databasemodel, add View-only properties and decorating the viewmodel with the attributes for the UI.</p>
  
  <pre><code>public class POCOWithoutDataAnnotations
  {
      public virtual string PleaseOverrideMe { get; set; }        
  } 
  public class EditModel : POCOWithoutDataAnnotations
  {
      [Required]
      [DisplayName("This should be as label for please override me!")]
      public override string PleaseOverrideMe 
      {
          get { return base.PleaseOverrideMe; }
          set { base.PleaseOverrideMe = value; }
      }
  
      [Required]
      [DisplayName("This property exists only in EditModel")]
      public string NonOverriddenProp { get; set; }
  }
  </code></pre>
  
  <p>The strongly typed <code>ViewPage&lt;EditModel&gt;</code> contains:</p>
  
  <pre><code>        &lt;div class="editor-label"&gt;
              &lt;%= Html.LabelFor(model =&gt; model.PleaseOverrideMe) %&gt;
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%= Html.TextBoxFor(model =&gt; model.PleaseOverrideMe) %&gt;
              &lt;%= Html.ValidationMessageFor(model =&gt; model.PleaseOverrideMe) %&gt;
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              &lt;%= Html.LabelFor(model =&gt; model.NonOverriddenProp) %&gt;
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              &lt;%= Html.TextBoxFor(model =&gt; model.NonOverriddenProp) %&gt;
              &lt;%= Html.ValidationMessageFor(model =&gt; model.NonOverriddenProp) %&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>The labels are then displayed as <em>"PleaseOverrideMe"</em> (<strong>not</strong> using the DisplayNameAttribute) and <em>"This property exists only in EditModel"</em> (<strong>using</strong> the DisplayNameAttribute) when viewing the page.<br>
  If I post with empty values, triggering the validation with this ActionMethod:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(EditModel model)
      {
          if (!ModelState.IsValid)
              return View(model);
          return View("Thanks");
      }
  </code></pre>
  
  <p>the <code>&lt;%= Html.ValidationMessageFor(model =&gt; model.PleaseOverrideMe) %&gt;</code> actually uses <code>[DisplayName("This should be as label for please override me!")]</code> attribute, and produces the default errortext <em>"The This should be as label for please override me! field is required."</em></p>
  
  <p>Would some friendly soul shed some light on this?</p>