1

Why is the following code not working:

@Html.DisplayFor(modelItem => item.SupplierName.ToString().Replace("\"", ""))

I get following error:

System.InvalidOperationException was unhandled by user code

Inner exception:

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

Is working with Templates, like here in the following question on SO: replace character in text generated by html.displayfor, the only solution?

Looks strange to me to write templates only for formatting some text?

Community
  • 1
  • 1
Mivaweb
  • 5,580
  • 3
  • 27
  • 53

1 Answers1

2

Html.DisplayFor is looking for a DisplayTemplate for the data type. I am providing a rough example of using the DisplayFor in your scenario, although likely not needed.

Create a DisplayTemplates folder under Views\Shared and add file called something link RemoveSlashes.cshtml.

@model string
@Html.Raw(Model.Replace("\"", ""))

Then you can use this display template on your fields.

@Html.DisplayFor(modelItem => item.SupplierName, "RemoveSlashes")

This will then pass your item to the DisplayTemplate and output the code in the display template.

Jerode
  • 490
  • 4
  • 15