Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't seem to make it work when trying to use template editors for editing a collection of objects. Here's what I have:</p> <pre><code>public class Candidate { public Candidate() { this.References = new List&lt;Reference&gt;(); } public int Id { get; set; } public string Name { get; set; } public DateTime? DateOfBirth { get; set; } [UIHint("Reference")] public List&lt;Reference&gt; References { get; set; } } public class Reference { public int Id { get; set; } public string Name { get; set; } public string Institution { get; set; } public string Email { get; set; } public string Phone { get; set; } public string Position { get; set; } } </code></pre> <p>I created a template in Views/Shared/TemplateEditors, and created a strongly typed partial view Register.ascx.</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Models.Reference&gt;" %&gt; &lt;%:Html.TextBoxFor(model=&gt;model.Name) %&gt; &lt;%:Html.TextBoxFor(model=&gt;model.Email) %&gt; &lt;%:Html.TextBoxFor(model=&gt;model.Institution) %&gt; &lt;%:Html.TextBoxFor(model=&gt;model.Position) %&gt; &lt;%:Html.TextBoxFor(model=&gt;model.Phone) %&gt; </code></pre> <p>In the view that I have the candidate I use the code below to render the list of references.</p> <pre><code> &lt;%: Html.EditorFor(m =&gt; m.References)%&gt; </code></pre> <p>When I run the project I get</p> <pre><code> InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Models.Reference]', but this dictionary requires a model item of type 'Models.Reference'. </code></pre> <p>Do you guys see anything wrong with the implementation?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload