Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

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("
", "<br />");
</span>

Result on page:

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

Should be displayed as:

Address Top
Street Name
City
PostCode
question from:https://stackoverflow.com/questions/27284734/render-a-string-as-html-in-c-sharp-razor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...