使用MVC3和剃刀总输出原始的HTML [英] Always output raw HTML using MVC3 and Razor

查看:116
本文介绍了使用MVC3和剃刀总输出原始的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,看起来像这样的属性的类:

I’ve got a class with a property that looks like this:

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

我已经把在 [AllowHtml] 属性让我通过我建立的形式提交HTML这个属性,但我想要做的输出属性因为没有它被转义原始HTML的价值。

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.

我知道我可以使用 Html.Raw(Model.Description)但我正在寻找的是讲的一些方法 Html.DisplayFor (M = GT; m.Description)来一直输出原始的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?

基本上,这是我的懒惰,我不希望一定要记住哪些属性可能含有HTML,所以我不想去想使用 Html.Raw(...),当我需要做上面我倒是更愿意我的模型知道什么应该做,并自动完成。我试图寻找一个答案,但无论是我没有正确的措辞,或有没有做这件事的方式:(

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 :(

谢谢,

推荐答案

更改说明 proerpty返回一个 HtmlString

Change your Description proerpty to return an HtmlString.

剃刀不逃避 HtmlString 值。结果
(事实上​​,所有 Html.Raw 确实是创建一个 HtmlString

Razor does not escape HtmlString values.
(In fact, all Html.Raw does is create an HtmlString)

这篇关于使用MVC3和剃刀总输出原始的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆