我在下面收到此错误。
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
下面在SUBSTRING()函数
中触发异常<td class="hidden-desktop">@Html.DisplayFor(modelItem => item.isim.Substring(0,10).ToString());</td>
<td class="hidden-phone hidden-tablet">@Html.DisplayFor(modelItem => item.isim)</td>
我正在尝试根据屏幕大小显示同一文本的短版和长版。我收到错误消息的错误是什么?或者我应该如何正确使用substring()?
答案 0 :(得分:2)
您不必将Html.DisplayFor用于简单的字符串属性。用以下内容替换您的代码:
<td class="hidden-desktop">@item.isim.Substring(0,10)</td>
<td class="hidden-phone hidden-tablet">@item.isim</td>
另一个更好的选择是在View Model中定义一个新的isimShort属性,并在Controller中将其设置为isim.Substring(0,10)。
答案 1 :(得分:1)
将其更改为
<td class="hidden-desktop">@Html.Display("isim", item.isim.Substring(0,10))</td>
DisplayFor
期望参数是属性,但Substring()
不是属性
或者只是
<td class="hidden-desktop">@item.isim.Substring(0,10)</td>
答案 2 :(得分:1)
尝试将其更改为:
<td class="hidden-desktop">@Html.DisplayFor(modelItem => modelItem.isim.Substring(0,10).ToString());</td>
<td class="hidden-phone hidden-tablet">@Html.DisplayFor(modelItem => modelItem.isim)</td>