35

I'm not quite sure how this works yet... trying to find documentation.

In my existing app I've got two different ways of rendering strings in my View

<%: model.something %>
<!-- or -->
<%= model.something %>

The first one is html encoded, and the second one is not.

Is there something similarly short in Razor? All I can find is this, which is the encoded version.

@model.something
Chase Florell
  • 42,985
  • 56
  • 169
  • 364

2 Answers2

57

I guess the best approach would be to use the Raw extension-method: @Html.Raw(Model.Something)

Magnus
  • 1,022
  • 8
  • 12
15

@Model.Something automatically HTML encodes. If you want to avoid HTML encoding (and you want this only if you are absolutely sure what you are doing) you could use @MvcHtmlString.Create(Model.Something) (basically everything that implements IHtmlString won't be encoded). Phil Haack blogged about the Razor view engine syntax.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • 1
    cool thanks. Do you know if they're working on a shorter syntax for sending a string unencoded? something like `@=Model.Something` would be nice. Not saying that is the right way, just some idea. – Chase Florell Nov 13 '10 at 17:15
  • 4
    We are looking at something like that for v2 of Razor. – marcind Nov 13 '10 at 18:10
  • I'll be ecstatic when this comes to fruition. In fact, it would be nice to be able to set this in a buddy class as well so that it's set and forget. `` for the `@Html.EditorFor()` – Chase Florell Dec 05 '10 at 19:43
  • 1
    +1 for the `IHtmlString` but not the answer. I prefer @Magnus answer which is `@Html.Raw(Model.Something)` – CallMeLaNN Mar 30 '11 at 08:10
  • I just received an email from Marcind stating that we should in fact be using `@Html.Raw()` – Chase Florell Apr 11 '11 at 17:04