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

前端 未结 4 482
渐次进展
渐次进展 2020-11-30 23:11

Why am I receiving the error:

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

4条回答
  •  死守一世寂寞
    2020-11-30 23:56

    I had the same problem with something like

    @foreach (var item in Model)
    {
        @Html.DisplayFor(m => !item.IsIdle, "BoolIcon")
    }
    

    I solved this just by doing

    @foreach (var item in Model)
    {
        var active = !item.IsIdle;
        @Html.DisplayFor(m => active , "BoolIcon")
    }
    

    When you know the trick, it's simple.

    The difference is that, in the first case, I passed a method as a parameter whereas in the second case, it's an expression.

提交回复
热议问题