65

I am attempting to render an address from my model. The string contains line breaks that I am replacing with a break tag. Although, it is rendering on the page as a string instead as HTML. How can I force my string to render as HTML instead?

Attempt:

<span id="addressLine">
    @Model.MyData.Address.Replace("\r\n", "<br />");
</span>

Result on page:

Address Top<br />Street Name<br />City<br />PostCode

Should be displayed as:

Address Top
Street Name
City
PostCode
5
  • 1
    you should split the string. Commented Dec 4, 2014 at 1:30
  • @DanielA.White - Sorry mate, first day using C#. So I should split it and save it as different variables and render them on different lines? Commented Dec 4, 2014 at 1:32
  • 3
    Use @Html.Raw(Model.MyData.Address.Replace("\r\n", "<br />")) Commented Dec 4, 2014 at 1:36
  • @Dom - Cheers, worked perfectly. Post as an answer. Commented Dec 4, 2014 at 1:37
  • <br/> inside <span>? Cringe! Commented Dec 4, 2014 at 1:48

5 Answers 5

100

Use @Html.Raw(Model.MyData.Address.Replace("\r\n", "<br />"))

Sign up to request clarification or add additional context in comments.

3 Comments

Be careful using this, as if the user's address is 1234 <script>alert('a')</script> lane, you will have xss problems
@JohnKoerner - Don't worry, we clean out everything and format the address before saving it in the database. Thanks for the heads up though.
This is no longer the best answer. A couple of answers below use a CSS/HTML strategy that avoids the use of Html.Raw. The problem with line-breaks is a CSS/HTML one, and modern browsers can be controlled (with CSS/HTML) to behave how you intend.
34

Use

@(new HtmlString(@Model.MyData.Address))

It is safer, so that you avoid potential xss attacks

See this post: Rendering HTML as HTML in Razor

1 Comment

How does it avoid potential xss attacks? It is just a wrapper around a string to indicate that when used on a view that the content shouldn't be encoded, so any untrusted content will be displayed the same as Html.Raw.
10

Use css to preserve the white space

Html

<div id="addressLine">
  @Model.MyData.Address;
</div>

Css

#addressLine {
  white-space: pre;
}

2 Comments

This answer is also better because you no longer need to use Html.Raw.
This answer is only about css, not about html encoding or rendering html as is with razor. Which happens serverside, so little to do with css in this case.
10

To render HTML on blazor you can use MarkupString => @((MarkupString)here your string) this will render the string as HTML

<span id="addressLine">
    @((MarkupString)@Model.MyData.Address.Replace("\r\n", "<br />"));
</span>

Comments

1

You should use CSS whitespace property instead of it. For more details, access http://www.w3schools.com/cssref/pr_text_white-space.asp

It also helps you avoiding Cross-site scripting (http://en.wikipedia.org/wiki/Cross-site_scripting)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.