简体   繁体   English

在字符串输出中允许html代码

[英]Allowing html code in string output

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. 该字符串包含一个' - ',我想用"<br />"替换它,以便它在破折号上打破。

 <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? 有没有办法强制休息?

Use HtmlHelper.Raw method: 使用HtmlHelper.Raw方法:

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

In your code this would be: 在您的代码中,这将是:

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

Similar to @walkhard's answer but provided in the context of your code. 与@ walkhard的答案类似,但在代码的上下文中提供。

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

Edit: Looks like you got it! 编辑:看起来你明白了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM