2011-10-03 66 views
0

我从使用谷歌地图的ASP.NET MVC写斯科特·米切尔的文章得到了下面的示例代码:JSON代ASP.NET MVC 3

@{ 

    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) 
    ); 
    } 
} 

然而,当页面呈现以下是说明(我加一个空格之间“&”和“QUOT;”

{ 
    title: & quot;Store #893& quot;, 
    position: new google.maps.LatLng(32.7178080, -117.1611020) 
} 

最有可能涉及到JSON编码,但我仍然在ASP.NET MVC初学者

回答

0

当输出的位置,不要只使用变量,但在@Html.Raw(i)包裹。这样渲染引擎就知道它不必转义可能危险的字符,如引号。在代码中,它应该看起来像

@for(var location in locations) 
{ 
    Html.Raw(location); 
} 

也看看JsonResult。比创建自己的json字符串更容易,更清洁。

0

尝试使用。 @:所以它doe sn't HTML编码输出

+0

我在哪里使用它? – Bill

+0

尝试locations.Add(string.Format( @:“{{ –

0

你应该尝试包裹要输出的JSON在MvcHtmlString