Search code examples
c#htmlasp.net-mvcrazor

Render a string as HTML in C# Razor


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

Solution

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