1

My view is

@model List<string>
...
@Html.DisplayForModel("Name")
...

My "Name" display template is

@model string
<span>@Model</span>

This isn't working, I am getting:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[string]', but this dictionary requires a model item of type 'string'`

Anything wrong I am doing here?

santhudr
  • 45
  • 6

1 Answers1

2

The model of your first list is of type List<string> and you pass this model to the display template. But the display template requires a model of type string. Your display template should also expect a list of strings:

@model List<string>
@foreach(var item in Model)
{
   <span>@item</span>
}
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
  • I thought DisplayFor() can be used to display the list of items in the model as answered here: http://stackoverflow.com/questions/11261590/mvc-razor-foreach – santhudr Sep 02 '14 at 12:05
  • @santhudr what if you change the model type of the view to `IEnumerable` and then call it with `@Html.DisplayFor(x => x, "Name")` instead of using `@Html.DisplayForModel("Name")`? – Kevin Brechbühl Sep 02 '14 at 12:13
  • I just found another post with same issue - http://stackoverflow.com/questions/8678802/why-is-my-displayfor-not-looping-through-my-ienumerabledatetime. It's because I have given the template name. @Html.DisplayForModel() works fine! :) – santhudr Sep 02 '14 at 14:06