2

My view displays a client's name at the top of every page using a layout template. This works fine except when the name property is assigned a value that contains an apostrophe. The view displays the ascii code for the apostrophe instead of an actual apostrophe. For example:

if the client's name is set to "Client's Name", the view renders the name as "Client's Name" instead of "Client's Name"

Layout Template code:

    @helper ClientName()
    {
        if (Session["CurrentClient"] != null)
        {                                
            @(((ClientModel)Session["CurrentClient"]).Name);
        }
        else
        {
            @String.Format("{0}", "None");
        }
    }

    @Html.ActionLink("Client[" + @ClientName().ToString() + "]", "Index", "Home", null, new { @class = "navbar-brand" })

What do I do to convert the string value to HTML so the apostrophe is shown?

McVenco
  • 1,011
  • 1
  • 17
  • 30
ihatemash
  • 1,474
  • 3
  • 21
  • 42
  • "The view renders the name as `"Client's Name"`" But you only see a apostrophe because `'` is an HTML entity, or does the HTML contain `'` and you actually see `'` on the screen? Are you sure that `Session["CurrentClient"]).Name` isn't already HTML encoded? – Ruan Mendes Dec 01 '15 at 17:54
  • I'm certain Session["CurrentClient"]).Name isn't already HTML encoded. The value is set to a string value from the database in the controller. So for a value of "King's Daughter", I'm actually seeing "King's Daughter" – ihatemash Dec 01 '15 at 18:16
  • 1
    See http://stackoverflow.com/a/10146258/227299. Also, post some proof that it's not already encoded. If you try `@Html.ActionLink(HttpUtility.HtmlDecode("Client[" + @ClientName().ToString() + "]"), "Index", "Home")` and it works, it would mean it's already encoded. – Ruan Mendes Dec 01 '15 at 18:32

1 Answers1

3

According to this answer ActionLink encodes what is passed in. Therefore, it's being double encoded.

Check the value of @ClientName().ToString(), that may already be encoded because helpers encode their output by default; you may need to un-encode it, or not encode it from your helper.

One solution is to make your helper not escape its content

@{ WriteLiteral(((ClientModel)Session["CurrentClient"]).Name); }

or

@Html.Raw(((ClientModel)Session["CurrentClient"]).Name))

If you must un-encode the results of @ClientName().ToString(), use

@Html.ActionLink(
   "Client[" + 
           HttpUtility.HtmlDecode(@ClientName().ToString()) +
           "]",
   "Index", "Home")
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • I guess it was encoded after all. I know the data from the database isn't encoded and that's why I insisted that it wasn't. I watched it in the debugger to make sure. Guess the ActionLink() was encoding it. Thanks for your help. – ihatemash Dec 01 '15 at 19:19
  • @ihatemash Can you please add a comment explaining how you fixed your issue, since I provided two possibilities? – Ruan Mendes Dec 01 '15 at 19:23
  • I used @Html.ActionLink("Client["+HttpUtility.HtmlDecode(@ClientName().ToString()) + "]", "Index", "Home") and that fixed the problem. – ihatemash Dec 01 '15 at 20:04