I’ve got the sample code below from an article written by Scott Mitchell on using Google Maps with ASP.NET MVC:
@{
var locations = new List<string>();
var infoWindowContents = new List<string>();
foreach (var store in Model) {
locations.Add(string.Format(
@"{{
title: ""Store #{0}"",
position: new google.maps.LatLng({1}, {2})
}}",
store.StoreNumber,
store.Latitude,
store.Longitude));
infoWindowContents.Add(string.Format(
@"{{
content: ""<div class=\""infoWindow\""><b>Store #{0}</b><br />{1}<br />{2}, {3} {4}</div>""
}}",
store.StoreNumber,
store.Address,
store.City,
store.Region,
store.PostalCode)
);
}
}
However, when the page renders the following is showing (I added a space between “&” and “quot;”
{
title: & quot;Store #893& quot;,
position: new google.maps.LatLng(32.7178080, -117.1611020)
}
Most probably related to JSON encoding, but I’m still a beginner in ASP.NET MVC.
When outputting the location, don’t just use the variable, but wrap it in
@Html.Raw(i). This way the render engine knows that it doesn’t have to escape possibly dangerous characters, like quotes. In code it should look something likeAlso look at JsonResult. Is is far easier and cleaner to use than creating your own json strings.