Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell razor NOT to html escape

I am using asp.net mvc 3 with razor for a project. At some I need to serialize an array from the controller, put it in the viewdata and assign it to a js object. However when I output it using

@ViewData["some array"] 

The result is html escaped so i get something like :

[{"title":"Something","id":"Something-1" etc' 

With the <%= %> this was not escaped so it was behaving as expected. Is it possible to tell razor not to escape this string. Perhaps, someone might suggest another approach all together.

Thanks in advance for any ideas

like image 576
sTodorov Avatar asked Dec 16 '10 16:12

sTodorov


People also ask

Why is HTML escaping?

Escapes are very useful for representing characters that are not apparent or are ambiguous. Numeric or named character references, as well as CSS escapes, can be used to represent characters in HTML style attribute. The style element HTML can not contain numeric or named character references.

How do you escape the razor page?

In Razor, `@` symbol is used to transition from HTML to C#. To escape an '@' symbol in razor markup, use two '@' symbols.

What is razor in HTML?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.


1 Answers

You need to output an instance of the new IHtmlString interface, which contains pre-escaped HTML.

To do that write @Html.Raw(...).

like image 60
SLaks Avatar answered Sep 28 '22 23:09

SLaks