3

I have a problem that is just making me feel silly.... Given the following code in Razor:

@{
    ...
    if (purchasedEvent.Address != null && purchasedEvent.Address != String.Empty){
        addressBlock.AppendFormat("{0}<br />", purchasedEvent.Address); 
    }
    ...
}
@addressBlock.ToString()

The <br /> gets treated as literal text (that is to say I end up seeing something like 123 Cool Street<br />Anytown... rendered on the page. Changing the code (back) to addressBlock.AppendLine(purchasedEvent.Address) doesn't do any good either (renders 127 Cool Street Anytown.... What do I need to do to make the Razor engine respect that line break?

user4593252
  • 3,496
  • 6
  • 29
  • 55
  • possible duplicate of [What is an MvcHtmlString and when should I use it?](http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it) – Erik Philips Jan 22 '15 at 18:15
  • I think the second answer on that does indeed answer this question but I'm not sure the question directly translates even though it is a far more expanded way of asking it. Thanks for the link. Think maybe I should take this one down? – user4593252 Jan 22 '15 at 18:37
  • 1
    I actually wouldn't. Duplicate questions worded differently are helpful for people trying to find an answer. Closed questions are not automatically deleted by the system. – Erik Philips Jan 22 '15 at 19:43
  • Honestly, that's what I was thinking/hoping but I'm not as familiar with SO protocol as others. Thanks again. – user4593252 Jan 22 '15 at 20:32

1 Answers1

3

You need to use Html.Raw. To quote the docs: "Returns markup that is not HTML encoded."

So something like

@html.Raw(addressBlock.ToString())

The reason for it is that MVC is assuming that what you are giving it is the text as you want it to be seen and thus HTML encodes it. Raw allows you to tell it not to do that.

Chris
  • 27,210
  • 6
  • 71
  • 92