简体   繁体   English

输出MVC ViewBag文本与一些未编码的区域?

[英]Outputting MVC ViewBag text with some areas not encoded?

Consider the following example: 请考虑以下示例:

<div>@ViewBag.MyData</div>

What to do if ViewBag.MyData contains a string with "\\n" (newlines) in it, that I want to change into " <br/> "? 如果ViewBag.MyData包含一个带有“\\ n”(换行符)的字符串,该怎么办,我想改成“ <br/> ”? When I include " <br/> " instead of the newlines, it gets encoded in the browser, which is bad. 当我包含“ <br/> ”而不是换行符时,它会在浏览器中编码,这很糟糕。

How can I help this? 我该怎么帮忙?

使用@Html.Raw(ViewBag.MyData.Replace("\\n", "</br>"))

the following will do what you need... 以下将做你需要的......

@MvcHtmlString.Create(ViewBag.MyData.Replace("\n", "</br>"))

The Create method will take a HTML string and render it as would be expected if you just typed the HTML straight onto the page 如果您只是直接在页面上键入HTML, Create方法将采用HTML字符串并按预期呈现它

That's what html pre tag is used for 这就是html pre标签的用途

The < pre> tag defines preformatted text. <pre>标签定义了预先格式化的文本。 Text in a < pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks ... <pre>元素中的文本以固定宽度字体(通常为Courier)显示,并保留空格和换行符...

And in razor template 并在剃刀模板

< pre>@ViewBag.MyData</ pre>

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

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