24

I have created a DisplayTemplate for a Comment class, and placed it inside Comment/DisplayTemplates/Comment.cshtml.

Comment.cshtml is properly typed:

@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:

@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:

The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.

How can I invoke the DisplayTemplate for each item in the foreach loop?

Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101

2 Answers2

34

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:

@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.

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Hah! Brilliant! I didn't know `DisplayFor` would automatically loop. Got rid of the view too, of course. Thanks! (I agree about the `Where` clause, I got lazy there for a minute ;) – Sergi Papaseit Apr 13 '11 at 16:36
  • @Sergi Papaseit, that's why everytime I see someone writing a loop in a view I always suggest him using templates. – Darin Dimitrov Apr 13 '11 at 16:38
  • Well, I certainly won't forget this one any more :) – Sergi Papaseit Apr 13 '11 at 16:41
  • @DarinDimitrov do you have any idea why this is not working in my case http://stackoverflow.com/questions/8678802/why-is-my-displayfor-not-looping-through-my-ienumerabledatetime – remi bourgarel Dec 30 '11 at 11:18
  • 2
    "Or if you really need such a view (don't know why)". `DisplayFor` can render the list, but it won't render a table. – Ark-kun Apr 11 '13 at 12:33
13

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).

In that case, DisplayFor can still be used within the loop like this:

@model IEnumerable<Comment>

@for (int index = 0; index < Model.Count(); index++)
{
     @Html.DisplayFor(model => model[index])
}
Fernando Neira
  • 3,944
  • 2
  • 24
  • 23
  • 5
    `IEnumerable` does not support implicit indexing with `[i]`. Logically then, you would try to use `model.ElementAt(i)` but then you get an error, so you can only do this by using `Comment[]` as the model type or `model.ToArray()[i]` – JoeBrockhaus Jul 01 '13 at 18:57