始终使用 MVC3 和 Razor 输出原始 HTML
我有一个类,其属性如下所示:
[AllowHtml]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
我已经放入 [AllowHtml]
属性,让我可以通过我构建的表单向此属性提交 HTML,但是我想要做的是将属性的值输出为原始 HTML,而不进行转义。
我知道我可以使用 Html.Raw(Model.Description)
但我正在寻找的是某种告诉 Html.DisplayFor(m => m.Description)
的方法> 始终输出原始 HTML。是否有一个属性可以用来装饰我希望表现得像这样的类中的属性?
基本上是我太懒了——我不想记住哪些属性可能包含 HTML,所以我不想在需要时考虑使用 Html.Raw(…)
执行上述操作 - 我更希望我的模型知道它应该做什么并自动执行。我尝试寻找答案,但要么我的措辞不正确,要么没有办法做到这一点:(
谢谢,
I’ve got a class with a property that looks like this:
[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.
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?
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. I’ve tried searching for an answer, but either I’m not phrasing it correctly or there’s no way of doing it :(
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改您的
Description
属性以返回HtmlString
。Razor 不会转义
HtmlString
值。(事实上,
Html.Raw
所做的只是创建一个HtmlString
)Change your
Description
proerpty to return anHtmlString
.Razor does not escape
HtmlString
values.(In fact, all
Html.Raw
does is create anHtmlString
)这实际上相当简单(一旦你知道如何......)。将您的 DataType 属性更改为
[DataType(DataType.Html)]
,并创建一个部分视图,将其放入Views/Shared/DisplayTemplates/Html.cshtml
中,如下所示:当然,您也可以不更改 DataType 属性,并将视图命名为
MultilineText.cshtml
而不是Html.cshtml
。This is actually rather simple (once you know how...). Change your DataType attrib to
[DataType(DataType.Html)]
, and create a partial view, put it inViews/Shared/DisplayTemplates/Html.cshtml
, with this:Of course you can also not change your DataType attrib, and name the view
MultilineText.cshtml
instead ofHtml.cshtml
.只是为了在这里提供更多信息 - 您的问题是 @ 将始终 HtmlEncode 除非您返回了 IHtmlString - 所以问题源于 @ 符号。这是 razor 语法的好处之一 - 进行 htmlencode 比不进行 htmlencode 更安全。因此,这里没有“快速”方法,因为问题的根源是 @ 符号,如果找到 IHtmlString,它将排除 HtmlEncoding。所以 - 没有“快速”的方法来解决这个问题,除非你使用旧的 <% 语法,恕我直言,与 razor 相比,它很糟糕:)
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. It's one of the benefits of the razor syntax - its safer to htmlencode than to not. So there is no 'quick' way here since the root of your issue is the @ symbol which will exclude HtmlEncoding if it finds IHtmlString. So - no 'quick' way around this unless you use the old <% syntax which IMHO sucks compared to razor : )
使用 MvcHtmlString(示例) 模型中的数据类型而不是字符串允许为我传递原始 html:
Using MvcHtmlString (examples) data type instead of string in the model allowed to pass raw html for me: