0

以下にこのエラーが表示されます。

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() を適切に使用するにはどうすればよいですか?

4

3 に答える 3

2

単純な文字列プロパティに Html.DisplayFor を使用する必要はありません。コードを次のように置き換えます。

<td class="hidden-desktop">@item.isim.Substring(0,10)</td>
<td class="hidden-phone hidden-tablet">@item.isim</td>

もう 1 つのより良いオプションは、ビュー モデルで新しい isimShort プロパティを定義し、コントローラーで isim.Substring(0,10) に設定することです。

于 2013-07-03T19:07:13.990 に答える