You must use the "EditorTemplate" using the interface in the same way that the class will use.
I have two problems:
I only recognize “EditorTemplate” if I include “DataAnnotation” “DataType” with the name “EditorTemplate”. This does not bother me much, but it would be nice to know it by type, as if it had an "EditorTemplate" with the name of my type (class). [DataType ("ICanBeListed")]
My interface uses generics to determine the type of list and return property. For this reason, I do not know how to make a template.
I know that there are two queries, but they do the same problem.
Archives:
Desired viewing model:
[DisplayName("Field one")] public FieldOne MyFieldOne { get; set; } [DisplayName("Field two")] public FieldTwo MyFieldTwo { get; set; }
Solution for point 1:
[DisplayName("Field one")] [DataType("ICanBeListed")] public FieldOne MyFieldOne { get; set; } [DisplayName("Field two")] [DataType("ICanBeListed")] public FieldTwo MyFieldTwo { get; set; }
Interface:
public interface ICanBeListed<T, U> { U Id { get; set; } string Description { get; set; } IList<T> ToList(); }
Classes:
public class FieldOne : ICanBeListed<FieldOne, string> { public string Id { get; set; } public string Descripcion { get; set; } public IList<FieldOne> List() { return new List<FieldOne> { new FieldOne{Id = "1", Descripcion = "Description 1"}, new FieldOne{Id = "2", Descripcion = "Description 2"} }; } } public class FieldTwo : ICanBeListed<FieldTwo, int> { public int Id { get; set; } public string Descripcion { get; set; } public IList<FieldTwo> List() { return new List<FieldTwo> { new FieldTwo{Id = 1, Descripcion = "Descripcion 1"}, new FieldTwo{Id = 2, Descripcion = "Descripcion 2"} }; } }
Template Editor:
@model Mvc3ConditionalValidation.Models.ICanBeListed<object, object> @Html.DropDownListFor(model => model.Id, new SelectList(Model.ToList(), "Id", "Description"))
View:
<div class="editor-field"> @Html.EditorFor(model => model.MyFieldOne) @Html.ValidationMessageFor(model => model.MyFieldOne) </div> <div class="editor-label"> @Html.LabelFor(model => model.MyFieldTwo) </div> <div class="editor-field"> @Html.EditorFor(model => model.MyFieldTwo) @Html.ValidationMessageFor(model => model.MyFieldTwo) </div>
I am trying to change the interface for a class:
View Model:
public class FieldTwo : CanBeListed<FieldTwo, int> { public override int Id { get; set; } public override string Description { get; set; } public override IList<FieldTwo> ForList() { return new List<FieldTwo> { new FieldTwo{Id = 1, Description = "Descripcion 1"}, new FieldTwo{Id = 2, Description = "Descripcion 2"} }; } }
Class (before interface):
public abstract class CanBeListed<T, U> { public virtual U Id { get; set; } public virtual string Description { get; set; } public abstract IList<T> ForList(); }
But the problem is the same:
The model element passed to the dictionary is of the type "Mvc3ConditionalValidation.Models.FieldOne", but this dictionary requires a model element of the type "Mvc3ConditionalValidation.Models.CanBeListed`2 [System.Object, System.Object] '.
thanks