title: ASP.NET MVC3 Editor Templates for interface or inherited classes
body: <p>Need to use a "EditorTemplate" employing an interface, in the same manner that would use a class.</p>
  
  <p>I have two problems:</p>
  
  <ol>
  <li><p>I only recognize the "EditorTemplate" if I include the "DataAnnotation" "DataType" with the name of "EditorTemplate". This does not bother me much, but it would be nice to be able to recognize it by type, as it did if it had a "EditorTemplate" with the name of my type (class). <code>[DataType ("ICanBeListed")]</code></p></li>
  <li><p>My interface uses generics to define the list and return type of property. For this reason I do not know how to do the template.</p></li>
  </ol>
  
  <p>I know there are two queries but do the same problem.</p>
  
  <p>Archives:</p>
  
  <p>Desirable view model:</p>
  
  <pre><code>[DisplayName("Field one")]
  public FieldOne MyFieldOne { get; set; }
  
  [DisplayName("Field two")]
  public FieldTwo MyFieldTwo { get; set; }
  </code></pre>
  
  <p>Solution for point 1:</p>
  
  <pre><code>[DisplayName("Field one")]
  [DataType("ICanBeListed")]
  public FieldOne MyFieldOne { get; set; }
  
  [DisplayName("Field two")]
  [DataType("ICanBeListed")]
  public FieldTwo MyFieldTwo { get; set; }
  </code></pre>
  
  <p>Interface:</p>
  
  <pre><code>public interface ICanBeListed&lt;T, U&gt;
  {
      U Id { get; set; }
  
      string Description { get; set; }
  
      IList&lt;T&gt; ToList();
  }
  </code></pre>
  
  <p>Classes:</p>
  
  <pre><code>public class FieldOne : ICanBeListed&lt;FieldOne, string&gt;
  {
      public string Id { get; set; }
  
      public string Descripcion { get; set; }
  
      public IList&lt;FieldOne&gt; List()
      {
          return new List&lt;FieldOne&gt;
          {
              new FieldOne{Id = "1", Descripcion = "Description 1"},
              new FieldOne{Id = "2", Descripcion = "Description 2"}
          };
      }
  }
  
  
  public class FieldTwo : ICanBeListed&lt;FieldTwo, int&gt;
  {
      public int Id { get; set; }
  
      public string Descripcion { get; set; }
  
      public IList&lt;FieldTwo&gt; List()
      {
          return new List&lt;FieldTwo&gt;
          {
              new FieldTwo{Id = 1, Descripcion = "Descripcion 1"},
              new FieldTwo{Id = 2, Descripcion = "Descripcion 2"}
          };
      }
  }
  </code></pre>
  
  <p>Template editor:</p>
  
  <pre><code>@model Mvc3ConditionalValidation.Models.ICanBeListed&lt;object, object&gt;
  @Html.DropDownListFor(model =&gt; model.Id, new SelectList(Model.ToList(), "Id", "Description"))
  </code></pre>
  
  <p>View: </p>
  
  <pre><code>&lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.MyFieldOne)
      @Html.ValidationMessageFor(model =&gt; model.MyFieldOne)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.MyFieldTwo)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.MyFieldTwo)
      @Html.ValidationMessageFor(model =&gt; model.MyFieldTwo)
  &lt;/div&gt;
  </code></pre>
  
  <p>I try to change the interface for a class:</p>
  
  <p>View model:</p>
  
  <pre><code>public class FieldTwo : CanBeListed&lt;FieldTwo, int&gt;
  {
      public override int Id { get; set; }
  
      public override string Description { get; set; }
  
      public override IList&lt;FieldTwo&gt; ForList()
      {
          return new List&lt;FieldTwo&gt;
          {
              new FieldTwo{Id = 1, Description = "Descripcion 1"},
              new FieldTwo{Id = 2, Description = "Descripcion 2"}
          };
      }
  }
  </code></pre>
  
  <p>Class (before interface):</p>
  
  <pre><code>public abstract class CanBeListed&lt;T, U&gt;
  {
      public virtual U Id { get; set; }
  
      public virtual string Description { get; set; }
  
      public abstract IList&lt;T&gt; ForList();
  }
  </code></pre>
  
  <p>But the problem is the same:</p>
  
  <p><strong>The model item passed into the dictionary is of type 'Mvc3ConditionalValidation.Models.FieldOne', but this dictionary requires a model item of type 'Mvc3ConditionalValidation.Models.CanBeListed`2[System.Object,System.Object]'.</strong> </p>
  
  <p>Thanks</p>