1

If I add some code like this:

<div>
    @Html.DisplayFor(x => x.SomeTextField)
</div>

...it is rendered like so...

<div>
    "
    SomeTextFieldsValue
    "
</div>

This is strange as it isn't using a tag as I would have expected... say, a label tag.

What is this use of double quotes in the html. Is it shorthand for a tag?

It's hard to find an answer by googling.


I ASKED THIS QUESTION A MOMENT AGO AND IT QUICKLY GOT MARKED AS PREVIOUSLY ANSWERED BY THIS THREAD (by Richard Everett, Reniuz, Soner Gönül):

What is the @Html.DisplayFor syntax for?

Please read my question before doing the same again and you will see IT IS NOT the same question at all?

enter image description here

Community
  • 1
  • 1
Beakie
  • 1,948
  • 3
  • 20
  • 46
  • 1
    Do you have any display template in your project? Also, please remove the last part of your question, it's irrelevant to your issue. – ken2k Nov 04 '14 at 12:57
  • 3
    The linked question explains how to customize the output of `Html.DisplayFor` which is pretty much what you want. If there is no display template for string in your project then it ism likely that your data contains "" – Ben Robinson Nov 04 '14 at 12:58
  • Question not the same but **did you read about `DisplayTemplates`**. – Renatas M. Nov 04 '14 at 12:58
  • I've never had that issue with DisplayFor; can you post the property and attributes for SomeTextField? Do you have a custom displaytemplate for this too? Can you post that markup? – Brian Mains Nov 04 '14 at 13:15
  • @ken2k It was the only way to stop it being marked as a duplicate... which it is not. – Beakie Nov 04 '14 at 13:36
  • @BenRobinson I am well aware how to customise DisplayFor... I am questioning why this standard method outputs a string with quotes? Is it valid HTML? – Beakie Nov 04 '14 at 13:37
  • SomeTextField is no more than a basic string property. Nothing special... – Beakie Nov 04 '14 at 13:38
  • @Reniuz Yes, I read that entire article. It has nothing to do with my question as far as I can see. It amazes me that you marked this as possible duplicate again??! – Beakie Nov 04 '14 at 13:43
  • Now how anyone can know whats going on on your side??! Cant you see many users thinking about same things. Prove that we are wrong, prove that DisplayTemplates nothing to do with it, prove that actual value has no quotes, make an example that we could reproduce the behavior and help you instead of spreading the anger. – Renatas M. Nov 04 '14 at 14:18
  • 1
    @Beakie It looks like the quotes in the screenshot are being inserted by chrome developer tools, they are not actually part of the HTML. Use view source instead or just look at the page instead of using the DOM inspector, you will see that they are not actually there, if they were they would show up on the normal browser view. – Ben Robinson Nov 04 '14 at 15:12

3 Answers3

1

The default display template used by DisplayFor for a string doesn't add double quotes in the generated HTML.

So I see two possibilities:

  • Value of SomeTextField contains those double quotes
  • You have in your project a custom DisplayTemplate for the type of SomeTextField (I assume string) that adds them
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • It neither contains double quotes nor is there a displaytemplate like this. If I use ValueFor I get the same output. These quotes are only html and not being displayed in the page render. – Beakie Nov 04 '14 at 13:40
  • @Beakie Could you try with `@Html.Raw(Model.SomeTextField)` to see what HTML is generated? – ken2k Nov 04 '14 at 13:43
  • I get exactly the same output. If I do @Model.MyField.Trim() I also get the same output. Can someone please post their own output of a string rendering so I can see that it is different? – Beakie Nov 04 '14 at 13:46
0

It's actually a chrome thing. Chrome is just showing where text exists.

Beakie
  • 1,948
  • 3
  • 20
  • 46
-4

I think you need LabelFor instead of DisplayFor. DisplayFor displaying template based on type you want to render.