18

我看过一些关于这个主题的帖子和一些博客,但似乎没有人提到我得到的输出。

我想要的是生成一个带有信息的谷歌地图地图。手动输入信息会产生正确的信息。所以那部分有效。

我被卡住的地方是当我要动态创建带有字符串的javascript数组时,该数组包含我想要在我的地图上的信息。

我想得到的html代码是:

<script type="text/javascript">     
    var projects = [
         ['Kantoor 4.1 bestaande bouw', 52.25446, 6.16024700000003, 'Deventer', '', 'adviseurs', 'rating30'],
         ['School nieuw 4.0', 52.243161, 4.43677860000003, 'Noordwijk', '', 'adviseurs', 'rating30'],   
    ];

非常简单的javascript数组,我想用它来创建:

<script type="text/javascript">
var projects = [
    @foreach (var item in Model)
    {
        @HttpUtility.JavaScriptStringEncode("['" + item.Gebouwnaam + "', " + item.LocatieLatitude.ToString().Replace(",", ".") + ", " + item.LocatieLongitude.ToString().Replace(",", ".") + ", '" + item.Plaats + "', '" + item.Gebruiksfunctie + "', '" + item.Licentiehouder + "', '" + item.rating + "'],");
     }
];
</script>

然而,这给了我:

<script type="text/javascript">
var projects = [
    [\u0027Kantoor 4.1 bestaande bouw\u0027, 52.25446, 6.16024700000003, \u0027Deventer\u0027, \u0027\u0027, \u0027adviseurs\u0027, \u0027rating30\u0027],
    [\u0027School nieuw 4.0\u0027, 52.243161, 4.43677860000003, \u0027Noordwijk\u0027, \u0027\u0027, \u0027adviseurs\u0027, \u0027rating30\u0027],
];  
</script>

转义单引号不起作用。我究竟做错了什么?

4

3 回答 3

17

刚试过

 <script type="text/javascript">
 var projects = [

   @Html.Raw("['" + "aaa" + "', '" + "bbb" + "'],")


 ];
 </script>

它工作并显示...

<script type="text/javascript">
var projects = [

       ['aaa', 'bbb'],


];
</script>
于 2012-07-12T10:35:00.633 回答
11

您不想在整个字符串上调用 JavaScriptStringEncode,这也会对您的文字指示符进行编码(在您的示例中将转换为 \u0027 )。相反,像这样在数组中的每个项目上调用它:

<script type="text/javascript">
var projects = [
    @foreach (var item in Model)
    {
        String.Format("['{0}',{1},{2},'{3}','{4}','{5}','{6}']",
                      HttpUtility.JavaScriptStringEncode(item.Gebouwnaam),
                      HttpUtility.JavaScriptStringEncode(item.LocatieLatitude.ToString().Replace(",", ".")),
                      HttpUtility.JavaScriptStringEncode(item.LocatieLongitude.ToString().Replace(",", ".")),
                      HttpUtility.JavaScriptStringEncode(item.Plaats),
                      HttpUtility.JavaScriptStringEncode(item.Gebruiksfunctie),
                      HttpUtility.JavaScriptStringEncode(item.Licentiehouder),
                      HttpUtility.JavaScriptStringEncode(item.rating)
         )
     }
];
</script>
于 2012-07-12T10:46:53.023 回答
2

我相信您可以在 .net 中完成大部分繁重的工作并利用 Html.Raw 为您转换对象:

@{
    var myObj = Model.Select(i => new {
        item.Gebouwnaam,
        item.LocatieLatitude.ToString().Replace(",", "."),
        item.LocatieLongitude.ToString().Replace(",", "."),
        item.Plaats,
        item.Gebruiksfunctie,
        item.Licentiehouder,
        item.rating }).ToArray();
}

<script type="text/javascript">
    var jsObj = @Html.Raw(Json.Encode(myObj));
</script>

由于在这个问题中涉及到, HttpUtility.JavaScriptStringEncode() 对于包含换行符的字符串非常方便:

@{ var myNetString = "Hi,\r\nMy name is Joe\r\nAnd I work in a button factory"; }

<script type='text/javascript'>
    var myJsString = '@HttpUtility.JavaScriptStringEncode(myNetString)';
</script>
于 2015-06-05T22:58:21.953 回答