I have a page that loads a viewmodel. In the viewmodel is a single property that contains a blob of JSON:
public string PageJson { get; set; }
In the view, I want to store it into a JavaScript variable, so I render it as:
<script>
var _pageJson= JSON.parse('@Html.Raw(Model.PageJson)');
</script>
The problem I'm having is that this JSON blob spans multiple lines, so when I try to assign it to a variable, it is giving me an error as if I tried to assign a JavaScript variable like this:
var _pageJson = '{
"PageCategories": [
{
"CategoryID": 4405958680,
"Description": "Advertising & Promotions"
},
//........code continues.........//
This of course results in an error after the first bracket since a string can't span multiple lines without either an ending quote and the +
symbol or the \
symbol. How can I get JavaScript to properly populate this variable? All I can think of is adding a \
after each line of the JSON in the controller but that seems absolutely ridiculous.