I am working on an MVC project and are passing a string to the view through the ViewData. The string has single quotes, double quotes and other special characters (to give some background, I am following the store locator tutorial as a test project, but using Razor instead of asp.net.)
As soon as the variable hits the Javascript tags, the special characters get decoded onto HTML. For example:
storeList = [{ title: 'Location # 12', position: new google.maps.LatLng(32.85914441, -96.9444170)},{ title: 'Location # 7', position: new google.maps.LatLng(32.855526, -96.9555080)}]....
When it should contain:
storeList = [{ title: 'Location # 12', position: new google.maps.LatLng(32.85914441, -96.9444170)},{ title: 'Location # 7', position: new google.maps.LatLng(32.855526, -96.9555080)}] ....
I have tried the solution listed on this question but have not been successful:
<script type="text/javascript">
var storeList = $("<div/>").html(@ViewData["storeList"]).text();
But I get an error regardless:
"Uncaught SyntaxError: Unexpected token &"
And the error starts at the ':
var storeList = $("<div/>").html([{ title: 'Location # 12', position: .....
Any help would be very appreciated. Thanks!