在 ViewData[“msg”] 内传递 NewLine(或
)
如何在 ViewData["msg"]
内传递 Environment.NewLine
(或
) 在视图本身内呈现为真实的
?
我正在使用 C#...
How do I pass an Environment.NewLine
(or a < br />
) inside a ViewData["msg"]
that renders as a real <br />
inside the View itself?
I'm using C# ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需使用
查看此处 了解更多详情。
编辑:
很抱歉,我一开始并没有理解这个问题。我以为你想对传递的字符串进行编码。有时我读问题的速度太快了:)
无论如何,要完全回答你的问题,请看看这些示例。假设在你的行动中你有:
并且在你看来你已经
注意到这两个结构之间的区别。您将得到以下结果:
所以第一个示例用法回答了您的问题。关键是不要对字符串进行编码,这与我一开始所理解的相反。第二个构造是使用
<%: %>
的构造,如果您使用 .NET 4.0,则可用,自动对其呈现的每个字符串进行编码。如果您仍然想使用这个最新的构造并避免对字符串进行编码,您必须使用一个技巧。只需告诉构造该字符串已经编码,不需要重新编码。要实现此目的,请使用此示例代码
,并在视图中通常使用
<%: %>
构造有关使用 MvcHtmlString 及其工作原理的更多详细信息,请参阅此 问题。
再见!
You can simply use
Have a look here for further details.
EDIT:
I am sorry but I did not understand the question at the beginning. I thought you wanted to encode the passed string. Sometimes I read question too fast :)
Anyway to completely answer your question have a look at these samples. Suppose in your action you have:
And in your view you have
Notice the difference between the two construct. You will get the following results:
So the first sample usage answer your question. The key is to simply not encoding the string as opposed to what I understood at the beginning. The second construct, the one that use
<%: %>
, available if you're using .NET 4.0, automatically encode every string it render.If you still want to use this latest construct and avoid that the string would be encoded you have to use a trick. Simply tell the construct that the string is already encoded and it does not need to be re-encoded. To achieve this use this sample code
and in the view normally use the
<%: %>
constructFor more details on using the MvcHtmlString and how it works please refer to this question on SO.
Ciao!
您是否在 ViewData 字符串本身中尝试了 \n 或 \r ?
Did you try \n or \r inside the ViewData string itself?
使用这个:
或者,使用变量:
Use this:
Or, with variables: