I have a custom editor in my EditorTemplates folder for a IList<PersonRelations>. The Editor has this model:
@model IList<PersonRelation>
and in my entity is as this:
public IList<PersonRelation> Relations { get; set; }
this is how I called it in my view:
<div class="editor-field">
@Html.EditorFor(model => model.Relations)
</div>
and it’s rendering the model if Relations is null.
But.. I want to declare my property in this way
private IList<PersonRelation> _relations;
public IList<PersonRelation> Relations
{
get { return _relations ?? (_relations = new List<PersonRelation>()); }
set { _relations = value; }
}
To avoid null references exceptions.
The thing is when the List is not null and has no elements, the editor is not being displayed at all.
In my editor I iterate through the elements but also I render another controls outside the loop, and I can’t see any elements.
I’m missing something?
Solved.
When I changed the property, I forgot to decorate it with
[UIHint("PersonRelations")]which was in the original form of the property (my custom editor’s file name is “PersonRelations.cshtml”)
This is needed due it seems that the engine is not able to infer the editor for a collection, even when you have one, so you explicitly have to tell which one you want to use.