Greg Hayden
Greg Hayden

Reputation: 135

What is the proper synatx using Mvc3 Razor for <%= %> from Mvc2

In attempting to update working code from Mvc2 to Mvc3 using the Razor engine, we found that this syntax no longer works.

<script type="text/javascript">
    var initialData = <% =  new JavaScriptSerializer().Serialize(Model) %>
</script>

A previous post indicated this to be "pretty trivial" but we are not finding that so. And the sample pointed to does not appear to use either json2 nor JavaScriptSerializer().

In the instant case we may choose to use an alternate method; however, it would still be valuable to know if the above line could/should work to transfer data from the @Model into a javascript variable.

Upvotes: 0

Views: 171

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

@Html.Raw() is equivalent to <%= %> and @Html.Encode() is equivalent to <%: %>

<script type="text/javascript"> 
    var initialData = @Html.Raw(new JavaScriptSerializer().Serialize(Model))
</script>

Upvotes: 4

Related Questions