Reputation: 1275
According to Microsoft's documention, I should be able to pass an anonymously typed object as additionalViewData when calling @Html.DisplayFor; however, when I do this, I receive a yellow screen stating:
The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[Surveys2.Models.ReportingSidebarItemViewModel]', but this dictionary requires a model item of type 'Surveys2.Models.ReportingSidebarItemViewModel'.
Here is part of my view:
@model Surveys2.Models.ReportingPageViewModel
@Html.DisplayFor(m => m.Pages, "ReportingSidebarItemViewModel", new { PageType = Model.PageType } )
Here is my controller action:
public ActionResult Summary(string projectCode)
{
ReportingPageViewModel reportingPageViewModel = GetReportingPageViewModel(new ReportingPageParams { ProjectCode = projectCode });
return View("Page", reportingPageViewModel);
}
Upvotes: 1
Views: 144
Reputation: 730
The problem here is that you're targeting the display page by using the second param of DisplayFor. When you call displayfor(m=> m.prop) without a target view the ViewEngineCollection looks for the best suited diplay page. When the best suited display page is only for a single item and you passed a list it will iterate for you. The targeted display for assumes you are passing the exact type of the page you are targeting and thus it breaks.
EDIT-- Interestingly enough the MSDN Docs don't talk about looping except on the DisplayFor(m=> m.prop) method
Upvotes: 1