17

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,

1
  • 1
    You can set your Description property to type IHtmlString. That should take care of it for you. Of course, depending on how you set the value it might not work for all cases. Commented Jun 2, 2011 at 15:07

4 Answers 4

21

Change your Description proerpty to return an HtmlString.

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. Problem I have when I do that is that Entity Framework doesn’t like having the properties defined as HtmlString type so I get loads of model errors when it creates the model.
@Dylan: You could make a separate wrapper property of type IHtmlString and tell EF to ignore it.
Yeah, I guess I could, although it seems like too much hassle if I’m honest about it! I guess if there’s no simple (ie. quick and involves as little code as possible) then I’ll just continue to use Html.Raw(…). Thanks though.
@Dylan: public IHtmlString HtmlDescription { get { return new HtmlString(Description); } } You could also swap names.
I've discovered that @Html.DisplayTextFor(model => model.HtmlDescription) also seems to work. I suppose using one of the Html helpers has its benefits over just using @Model.HtmlDescription (for instance if the property is null).
|
5

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:

@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.

1 Comment

Been looking for that datatype thing. I just didn't think of it.
2

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

1 Comment

Yep, it certainly does. Other than @SLaks’ suggestion, there’s no easy way around it, so I’ll just leave it as it is and use Html.Raw(…) as I’ve got far too many properties to have to go and write duplicate versions just for the sake of not using Html.Raw(…)!!
0

Using MvcHtmlString (examples) data type instead of string in the model allowed to pass raw html for me:

//escaped string
public string cLiteral { get; set; }

//raw html string
public MvcHtmlString labelMessage { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.