Display string as HTML in C # Razor

I am trying to make an address from my model. The line contains line breaks, which I replace with the break tag. Although, this is turning the page as a string instead of HTML. How can I make my string display instead of HTML?

Attempt

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

Result on the page:

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

Should display as:

 Address Top Street Name City PostCode 
+5
source share
4 answers

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

+10
source

Using

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

See this post: Rendering HTML as HTML in Razor

+2
source

Instead, you should use the whitespace CSS property. For more information, contact http://www.w3schools.com/cssref/pr_text_white-space.asp

It also helps to avoid cross-site scripting ( http://en.wikipedia.org/wiki/Cross-site_scripting )

+1
source

Use css to save space

HTML

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

Css

 #addressLine { white-space: pre; } 
0
source

Source: https://habr.com/ru/post/1208310/


All Articles