Suppose I have the view models below.
public class CustomerListViewModel {
public IEnumerable<CustomerViewModel> Customers { get; set; }
}
public class CustomerViewModel {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
I want to display the list of Customers in my view inside a for-each loop. I also want to use DisplayFor to show the values with conventions applied (as opposed to just outputting the property as-is: customer.Age). With ASP.NET MVC, I used to just ignore the lambda variable in my expression (as was similarly discovered by the asker of this question).
<% foreach (var customer in Model.Customers) { %>
...
<li><%: this.DisplayFor(m => customer.Age) %></li>
<% } %>
However, I get an error with FubuMVC if I do this.
Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'.
Must I use a partial to render each customer with DisplayFor appropriately?
Thanks in advance!
In this particular use of display for:
<%: this.DisplayFor(m => customer.Age) %>
The overload is looking for an Expression that refers to a member of m. The use of "customer" here is what is causing the reflection exception.
There is an additional overload here: https://github.com/DarthFubuMVC/fubumvc/blob/master/src/FubuMVC.Core/UI/FubuPageExtensions.cs#L229
This one lets you explicitly specify the model type and instance:
<%: this.DisplayFor<Customer>(customer, c => c.Age) %>