我的EditorTemplates文件夹中有一个IList<PersonRelations>
的自定义编辑器。编辑有这个模型:
@model IList<PersonRelation>
在我的实体中是这样的:
public IList<PersonRelation> Relations { get; set; }
这就是我在观点中称之为的方式:
<div class="editor-field">
@Html.EditorFor(model => model.Relations)
</div>
如果Relations
为空,则呈现模型。
但是..我想以这种方式申报我的财产
private IList<PersonRelation> _relations;
public IList<PersonRelation> Relations
{
get { return _relations ?? (_relations = new List<PersonRelation>()); }
set { _relations = value; }
}
避免空引用异常。
事情是当List不为null且没有元素时,编辑器根本没有显示。
在我的编辑器中,我遍历元素,但我也在循环外渲染另一个控件,我看不到任何元素。
我错过了什么?
答案 0 :(得分:0)
解决。
当我更改属性时,我忘了用[UIHint("PersonRelations")]
是属性的原始形式(我的自定义编辑器的文件名是“PersonRelations.cshtml”)
这是必要的,因为似乎引擎无法推断集合的编辑器,即使你有一个集合,所以你必须明确告诉你要使用哪一个。