title: Using abstract view model in MVC 3
body: <p>I have an input form containing several input fields. Each input field has a <code>ElementModel</code> which has properties basically for the label and the value. The input fields to display are specified in a XML document, so I have kind of a dynamic view with only a list of elements. </p>
  
  <p>The problem is, that each element should be either displayed as a decimal or as percentage value. And of course, if it's a percentage value, the user shoud be able to input something like "45%" and the value in the model should then be 0.45.</p>
  
  <p>My first thought when I found <a href="https://stackoverflow.com/questions/7234966/percentage-properties-in-mvc-3">this</a> article was to use an abstract view model class with an abstract property for the value and to define a <code>PercentageElementModel</code> deriving from my base <code>ElementModel</code>class that makes use of a custom model binder. Unfortunately, if I use that abstract base class in my view, the data annotations made in the <code>PercentageElementModel</code>are ignored.</p>
  
  <p>Do you have any idea of how I can solve this? I don't want to use strings in my view model and do the parsing by myself as this will break the MVC pattern. Are there some other ways to achieve my goal?</p>
  
  <p>Here are some code snippets:</p>
  
  <pre><code>public abstract class ElementModel
  {
      public string ElementName { get; set; }
  
      public ElementType ElementType { get; set; }
  
      public abstract double? ElementValue { get; set; }
  }
  
  public class PercentageElementModel : ElementModel
  {
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:P2}")]
      public override double? ElementValue { get; set; }
  }
  </code></pre>