Arif YILMAZ
Arif YILMAZ

Reputation: 5866

mvc4 System.InvalidOperationException

I am getting this error below.

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.

the exception is triggered below in the SUBSTRING() function

<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>

I am trying to diisplay the short and long versions of the same text according to the screen size. what am I doing wrong to get the error message? or how should I use the substring() properly?

Upvotes: 0

Views: 1430

Answers (3)

codingbiz
codingbiz

Reputation: 26386

Change it to

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

DisplayFor expect the argument to be property but Substring() is not a property

Or just

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

Upvotes: 1

ataravati
ataravati

Reputation: 9155

You don't have to use Html.DisplayFor for a simple string Property. Replace your code with the following:

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

The other and better option is to define a new isimShort Property in your View Model, and set it to isim.Substring(0,10) in your Controller.

Upvotes: 2

hutchonoid
hutchonoid

Reputation: 33306

Try changing it to this:

<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>

Upvotes: 1

Related Questions