我已经为Comment类创建了一个displayTemplate,并将它放在Comment / displayTemplates / Comment.cshtml中。
Comment.cshtml正确键入:
@model Comment
然后,我有一个局部视图,它需要一个IEnumerable< Comment>为模型。在那里我循环遍历集合,并希望使用displayTemplate作为Comment类。这个观点,其诚信:
@model IEnumerable<Comment> @foreach (var comment in Model.Where(c => c.Parent == null)) { @Html.displayFor(model => comment) }
The model item passed into the dictionary is of type ‘system.int32’,but this dictionary requires a model item of type ‘System.String’.
解决方法
而不是有一个看法,采取IEnumerable<注释>并且它所做的只是循环遍历集合并简单地调用适当的显示模板:
@Html.displayFor(x => x.Comments)
Comments属性是IEnumerable< Comment>这将自动执行循环并为该集合的每个项目呈现Comment.cshtml显示模板。
或者如果你真的需要这样的观点(不知道为什么),你可以简单地:
@model IEnumerable<Comment> @Html.displayForModel()
就你所在的Where子句而言,你应该简单地删除它并将这个任务委托给控制器。准备视图模型是控制器的责任,而不是执行这些任务的视图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。