Samuel Jack Samuel Jack - 6 months ago 51
JSON Question

How do I write unencoded Json to my View using Razor?

I'm trying to write an object as JSON to my Asp.Net MVC View using Razor, like so:

<script type="text/javascript">
var potentialAttendees = @Json.Encode(Model.PotentialAttendees);

The problem is that in the output the JSON is encoded, and my browser doesn't like it. For example:

<script type="text/javascript">
var potentialAttendees = [{&quot;Name&quot;:&quot;Samuel Jack&quot;},];

How do I get Razor to emit unencoded JSON?

Answer Source

You do:


In releases earlier than Beta 2 you did it like:

@(new HtmlString(Json.Encode(Model.PotentialAttendees)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download