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?

1
  • 1
    ok if you use @Html.Raw(item.SupplierName.ToString().Replace("\"", "")), it's working! Commented Jun 23, 2015 at 14:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.