The below line appears in one of my javascript files, what would be the syntax for it in Razor.
var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;
The below line appears in one of my javascript files, what would be the syntax for it in Razor.
var initialData = <%= new JavaScriptSerializer().Serialize(Model) %>;
Like this:
@Html.Raw(new JavaScriptSerializer().Serialize(Model))
The Html.Raw
call is necessary to prevent it from being HTML-escaped.
For a more succinct feel, you can use the Web Pages Json helper's Encode method:
var initialData = @Html.Raw(Json.Encode(Model))