title: Validating dropdown in MVC4, data-val attributes not generated
body: <p>I am having horrific difficulty in getting unobtrusive validation working on a dropdown list in MVC4. </p>
  
  <p>The main symptom that I am seeing is that the data-val attributes are not rendered on the dropdown element.</p>
  
  <p>The root cause is that the DropDownFor is looking at the editor template's model which does not have any validation attributes on it. The validation is on the parent object property / main model. I am using the dropdown implementation suggested by @DarinDimitrov in this post: <a href="https://stackoverflow.com/a/17760548/89092">https://stackoverflow.com/a/17760548/89092</a></p>
  
  <p><strong>Does anybody know of a pattern for implementing a dropdown box based on a model value that is a list of key pairs with a selected flag AND having this unobtrusively validated?</strong></p>
  
  <p>I'd love to be able to call <code>Html.GetUnobtrusiveValidationAttributes()</code> in the editor template, append the attributes and keep the code that I have - but for the life of me I can't figure out how to get any data from it - I think it is because the ViewData.ModelMetadata that I have at this point is scoped to the DropDownValues type and not to the main model that has the decorated property test_dd, but if anybody knows what riddle to feed into this little black box then that would be great. </p>
  
  <p>My Model:</p>
  
  <pre><code>...
  [Required]
  [UIHint("SelectListItemDD")]    
  public DropDownValues test_dd { get; set; }
  ...
  </code></pre>
  
  <p>This is the DropDownValues view model</p>
  
  <pre><code>public class DropDownValues : IDropDownValues
  {
      public string SelectedValue { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Items { get; set; }
  }
  </code></pre>
  
  <p>My Editor Template view model</p>
  
  <pre><code>@model  DropDownValues
  @{
      string initial = (ViewData.ModelMetadata).AdditionalValues["InitialValue"] as string;
  }
  @{if (Model != null &amp;&amp; Model.Items != null &amp;&amp; Model.Items.Count() &gt; 0)
    {
      @Html.DropDownListFor(x =&gt; x.SelectedValue,
      new SelectList(Model.Items, "Value", "Text"), initial)
    }
  }
  </code></pre>