Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions

This part of code is not working

@foreach (var item in Model) {
        <tr>
            <td>
            @Html.DisplayFor(modelItem=>item.Registrations.Count())
         </td>

and throws an error

[InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.]

But this part of code below is working as a charm.

@foreach (var item in Model) {
    <tr>
        <td>
           @item.Registrations.Count()
        </td>

Is there anybody who can explain why is so?

like image 539
Zenon Iwanicki Avatar asked Mar 29 '12 12:03

Zenon Iwanicki


1 Answers

I'd think it's because you're using modelItem => item.Registrations.Count(). DisplayFor wants an expression that refers to a member of your model, not a function on a member's method as the model binder wouldn't know how to serialize the data on the way back (i.e. after a form post).

EDIT: I'd weirdly had the same error but for a completely different reason which turned out to be that the DateTime needed to be nullable

like image 139
Paul Carroll Avatar answered Nov 14 '22 05:11

Paul Carroll