简体   繁体   English

始终使用 MVC3 和 Razor 的 output 原始 HTML

[英]Always output raw HTML using MVC3 and Razor

I've got a class with a property that looks like this:我有一个 class ,其属性如下所示:

[AllowHtml]
[DataType(DataType.MultilineText)]
public string Description { get; set; }

I've already put in the [AllowHtml] attribute to let me submit HTML to this property via the form that I've built, but what I want to do is output the value of the property as the raw HTML without it being escaped.我已经输入了[AllowHtml]属性,让我通过我构建的表单将 HTML 提交给该属性,但我想要做的是 output 属性的值作为原始 Z4C4AD5FCA2E07A3F7ZZDBB1 转义而不转义。

I know I can use Html.Raw(Model.Description) but what I'm looking for is some way of telling Html.DisplayFor(m => m.Description) to always output the raw HTML. I know I can use Html.Raw(Model.Description) but what I'm looking for is some way of telling Html.DisplayFor(m => m.Description) to always output the raw HTML. Is there an attribute I can use to decorate the properties in my class that I wish to behave like that?是否有一个属性可以用来装饰我希望这样做的 class 中的属性?

Basically it's me being lazy—I don't want to have to remember which properties might contain HTML, so I don't want to have to think about using Html.Raw(…) when I need to do the above—I'd much rather my Model know what it should do and do it automatically.基本上是我懒惰——我不想记住哪些属性可能包含 HTML,所以我不想在需要执行上述操作时考虑使用Html.Raw(…)更确切地说,我的 Model 知道它应该做什么并自动执行。 I've tried searching for an answer, but either I'm not phrasing it correctly or there's no way of doing it:(我试过寻找答案,但要么我的措辞不正确,要么没有办法:(

Thanks,谢谢,

Change your Description proerpty to return an HtmlString .更改您的Description属性以返回HtmlString

Razor does not escape HtmlString values. Razor 不会转义HtmlString值。
(In fact, all Html.Raw does is create an HtmlString ) (事实上,所有Html.Raw所做的就是创建一个HtmlString

This is actually rather simple (once you know how...).这实际上相当简单(一旦你知道如何......)。 Change your DataType attrib to [DataType(DataType.Html)] , and create a partial view, put it in Views/Shared/DisplayTemplates/Html.cshtml , with this:将您的 DataType 属性更改为[DataType(DataType.Html)] ,并创建一个局部视图,将其放入Views/Shared/DisplayTemplates/Html.cshtml ,其中:

@model string
@Html.Raw(Model)

Of course you can also not change your DataType attrib, and name the view MultilineText.cshtml instead of Html.cshtml .当然,您也可以不更改 DataType 属性,并将视图命名为MultilineText.cshtml而不是Html.cshtml

Just to provide a bit more info here - your issue is that @ will always HtmlEncode unless you have IHtmlString returned - so the issue is sourced at the @ symbol.只是在这里提供更多信息 - 你的问题是 @ 将始终 HtmlEncode 除非你返回 IHtmlString - 所以问题源于 @ 符号。 It's one of the benefits of the razor syntax - its safer to htmlencode than to not.这是 razor 语法的好处之一 - htmlencode 比没有更安全。 So there is no 'quick' way here since the root of your issue is the @ symbol which will exclude HtmlEncoding if it finds IHtmlString.所以这里没有“快速”的方法,因为你的问题的根源是@符号,如果它找到 IHtmlString,它将排除 HtmlEncoding。 So - no 'quick' way around this unless you use the old <% syntax which IMHO sucks compared to razor: )所以 - 没有“快速”解决这个问题的方法,除非你使用旧的 <% 语法,恕我直言,与 razor 相比:)

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

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