NikolaiDante
NikolaiDante

Reputation: 18649

MVC Label with html entities

I have some label text with html entity characters e.g.   (and é etc) which when passed to Html.Label are coming out as   for example. What is the best way to get back   from the helper?

Edit: The text is coming from a localized resource, so for the case Html.Label("myinput", labelText) the labelText is unknown and may or may not contain entities.

Upvotes: 4

Views: 1631

Answers (2)

Marcin Zablocki
Marcin Zablocki

Reputation: 10693

I know it's a really old question, however it was never answered properly and I was looking for an answer to it.

The solution for this case is:

@Html.Label("myinput", HttpUtility.HtmlDecode("…yet another label"))

or if you are using model:

@Html.LabelFor(model=>model.SomeProperty,HttpUtility.HtmlDecode("…yet another label"))

of course if your label string does not contain any html entities, it will render just fine, so it covers your case with dynamic label:

@Html.Label("myinput", labelText)

Upvotes: 2

archil
archil

Reputation: 39501

For me, it's just simpler to write pure html

<label for="myinput">This is text&nbsp;</label>

Upvotes: 1

Related Questions