Thursday, 22 August 2013

Display template not accepting list from another property on the model?

Display template not accepting list from another property on the model?

I have a model that looks like this:
public class WorksheetViewModel() {
public Worksheet Worksheet {get; set;}
// ... more properties
}
public class Worksheet() {
public List<LineItem> LineItems {get; set;}
public Loan Loan {get; set; }
// ... more properties
}
public class LineItem() {
// ... some properties
}
public class Loan() {
public List<LoanField> Fields {get; set;}
// ... more properties
}
public class LoanField() {
// ... some properties
}
In my view I have the following:
@Html.DisplayFor(m => m.Worksheet.LineItems) // this works great
@Html.DisplayFor(m => m.Worksheet.Loan.Fields) // this throws exception
I'm getting an exception that looks like this:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[DataContracts.LoanField]',
but this dictionary requires a model item of type
'DataContracts.LoanField'.
The display templates for both collections are similar in that both
reference on the model they are are showing:
Working template:
@model DataContracts.LineItem
@Html.Label(Model.ItemType.Description) :
@Model.RecipientEmployee.FullName<br />
Not working template:
@model DataContracts.LoanField
@Html.Label(Model.FieldName) : @Model.FieldValue<br />
Since the first template correctly accepts a List of LineItem objects then
it would seem that the second template would behave the same way, but
clearly its not in this case.
Anyone have an idea why the template is complaining about the list and a
way to get around it?

No comments:

Post a Comment