asp.net-mvc 为集合中的每个项目使用 DisplayTemplate(与 DisplayFor)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5651697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a DisplayTemplate (with DisplayFor) for each item in a collection
提问by Sergi Papaseit
I have created a DisplayTemplate for a Comment
class, and placed it inside Comment/DisplayTemplates/Comment.cshtml
.
我为一个Comment
类创建了一个 DisplayTemplate ,并将它放在Comment/DisplayTemplates/Comment.cshtml
.
Comment.cshtml
is properly typed:
Comment.cshtml
正确键入:
@model Comment
Then, I have a partial view that takes an IEnumerable<Comment>
for model. In there I loop through the collection and would like to use the DisplayTemplate for the Comment
class. The view, in its integrity:
然后,我有一个采用IEnumerable<Comment>
for 模型的局部视图。在那里我循环遍历集合并希望使用 DisplayTemplateComment
类。该视图的完整性:
@model IEnumerable<Comment>
@foreach (var comment in Model.Where(c => c.Parent == null)) {
@Html.DisplayFor(model => comment)
}
However, I get an error on the Html.DisplayFor
line:
但是,我在线上收到错误消息Html.DisplayFor
:
The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.
传递到字典中的模型项的类型为“System.Int32”,但该字典需要类型为“System.String”的模型项。
How can I invoke the DisplayTemplate for each item in the foreach
loop?
如何为foreach
循环中的每个项目调用 DisplayTemplate ?
回答by Darin Dimitrov
Instead of having a view that take an IEnumerable<Comment>
and that all it does is loop through the collection and call the proper display template simply:
而不是拥有一个采用 an 的视图,IEnumerable<Comment>
它所做的只是循环遍历集合并简单地调用正确的显示模板:
@Html.DisplayFor(x => x.Comments)
where the Comments property is an IEnumerable<Comment>
which will automatically do the looping and render the Comment.cshtml
display template for each item of this collection.
其中 Comments 属性是一个IEnumerable<Comment>
,它将自动执行循环并Comment.cshtml
为该集合的每个项目呈现显示模板。
Or if you really need such a view (don't know why) you could simply:
或者,如果您真的需要这样的视图(不知道为什么),您可以简单地:
@model IEnumerable<Comment>
@Html.DisplayForModel()
As far as the Where
clause you are using in there you should simply remove it and delegate this task to the controller. It's the controller's responsibility to prepare the view model, not the view performing such tasks.
至于Where
您在那里使用的子句,您应该简单地将其删除并将此任务委托给控制器。准备视图模型是控制器的责任,而不是执行此类任务的视图。
回答by Fernando Neira
While the accepted answer works well most of the time, there are other cases in which we need to be aware of the element's index when rendering (i.e. add custom javascript that generates references to each element based on their index).
虽然接受的答案在大多数情况下运行良好,但在其他情况下,我们需要在渲染时了解元素的索引(即添加自定义 javascript,根据其索引生成对每个元素的引用)。
In that case, DisplayFor can still be used within the loop like this:
在这种情况下, DisplayFor 仍然可以像这样在循环中使用:
@model IEnumerable<Comment>
@for (int index = 0; index < Model.Count(); index++)
{
@Html.DisplayFor(model => model[index])
}