title: Reusable editor template with DropDownList for business objects
body: <p>I'm using MVC3 with Razor views and would like to build reusable DropDownLists for several of my classes, but after much searching I have not found an example that performs exactly how I need it...</p>
  
  <p>For this example I have two classes like this:-</p>
  
  <pre><code>public class Person
  {
    public int ID { get; set; }
    public string Name { get; set; }
    public Group Group { get; set; }
  }
  
  public class Group
  {
    public int ID { get; set; }
    public string Name { get; set; }
  }
  </code></pre>
  
  <p>I have a working Controller/View for Person. The view has a DropDownListFor control:</p>
  
  <pre><code>@model Person
  
  ...
  
  @Html.DropDownListFor(o =&gt; o.Group.ID, (ViewData["groups"] as SelectList))
  </code></pre>
  
  <p>The view uses the Person class directly, not an intermediary model, as I haven't found a compelling reason to abstract one from the other at this stage.</p>
  
  <p>The above works fine... in the controller I grab the value from Group.ID in the Person returned from the view, look it up, and set Person.Group to the result. Works, but not ideal.</p>
  
  <p>I've found a binder here: <a href="https://stackoverflow.com/questions/5657826/mvc-dropdownlist-values-posted-to-model-arent-bound">MVC DropDownList values posted to model aren&#39;t bound</a> that will work this out for me, but I haven't got that working yet... as it only really seems useful if I can reuse.</p>
  
  <p>What I'd like to do is have something like this in a template:-</p>
  
  <pre><code>@model Group
  
  @Html.DropDownListFor(o =&gt; o.Group.ID, (ViewData["groups"] as SelectList))
  </code></pre>
  
  <p>And use it in a view like this:-</p>
  
  <pre><code>@Html.EditorFor(o =&gt; o.Group)
  </code></pre>
  
  <p>However the above doesn't seem to work... the above EditorFor line inserts editors for the whole class (e.g. a textbox for Group.Description as well)... instead of inserting a DropDownList with my groups listed</p>
  
  <p>I have the above template in a file called Group.cshtml under Views/Shared/EditorTemplates</p>
  
  <p>If this worked, then whenever a class has a property of type Group, this DropDownList editor would be used by default (or at least if specified by some attribute)</p>
  
  <p>Thanks in advance for any advice provided...</p>