Search code examples
asp.net-mvcasp.net-mvc-4html-helper

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.


Solution

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