使用asp.net MVC的 HtmlHelper 时遇到的小问题,报错:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

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

解决办法:


由以下代码:
@foreach (var item in Model)
{
    @Html.DisplayFor(m => !item.IsIdle, "BoolIcon")
}
改为: @foreach (var item in Model) { var active = !item.IsIdle; @Html.DisplayFor(m => active , "BoolIcon") }

 或者

由以下代码:

<div class="controls" style="20%">
@{ int index = @Model.chapter_index + 1;}
@Html.TextBoxFor(model => index, new { @class = "span11" })
</div>

改为:

<div class="controls" style="20%">
@{ int index = @Model.chapter_index + 1;}
@Html.TextBoxFor(model => index, new { @class = "span11" })
</div>
原文地址:https://www.cnblogs.com/dayang12525/p/10277861.html