2

On a razor page I am displaying a string. The string contains a '-' that I would like to replace with a "<br />" so that it breaks on the dash.

 <h3>@Model.Location.Name.Replace(" - ","<br />")</h3>

This of course doesn't work because the system encodes the output so I get

Mylocation<br />MyLocation2

instead of the desired break.

Is there a way to force the break?

1

2 Answers 2

5

Use HtmlHelper.Raw method:

@Html.Raw("<br />")

In your code this would be:

<h3>@Html.Raw(@Model.Location.Name.Replace(" - ","<br />"))</h3>
Sign up to request clarification or add additional context in comments.

1 Comment

So I ended up with this '<h3>@Html.Raw(@Model.Location.Name.Replace(" - ","<br />"))</h3>' Worked like a charm.
2

Similar to @walkhard's answer but provided in the context of your code.

<h3>@Html.Raw(@Model.Location.Name.Replace(" - ","<br />"))</h3>

Edit: Looks like you got it!

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.