【问题标题】:My editor templates for an empty list is not rendering我的空列表编辑器模板未呈现
【发布时间】:2013-01-11 10:04:59
【问题描述】:

我的 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 且没有元素时,编辑器根本不会显示。

在我的编辑器中,我迭代了元素,但我也在循环之外渲染了另一个控件,但我看不到任何元素。

我错过了什么?

【问题讨论】:

    标签: razor asp.net-mvc-4


    【解决方案1】:

    解决了。 换房的时候忘了用[UIHint("PersonRelations")]装饰一下

    这是属性的原始形式(我的自定义编辑器的文件名是“PersonRelations.cshtml”)

    这是必需的,因为引擎似乎无法推断集合的编辑器,即使您有一个集合,所以您必须明确告诉您要使用哪个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多