如何在剃刀中创建一个 javascript 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11449782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:16:36  来源:igfitidea点击:

How to create a javascript string in razor

javascriptasp.net-mvc-3razor

提问by Dani?l Tulp

I have seen some posts regarding this topic and a few blogs, but none seem to mention the output I'm getting.

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

What I want is to generate a google maps map with information on it. Manually entering the information results in the correct information. So that part works.

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

Where I'm getting stuck is when I'm going to dynamiccaly create the javascript array with the string with the information I want on my map.

我遇到困难的地方是我要动态创建带有字符串的 javascript 数组,其中包含我想要在地图上显示的信息。

The html code I want to get is:

我想得到的 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'],   
    ];

Very simple javascript array, which I thought to create with:

非常简单的 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>

However this gives me:

然而,这给了我:

<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>

Escaping the single quotes doesn't work. What am I doing wrong?

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

采纳答案by Yasser Shaikh

Just tried with

刚试过

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

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


 ];
 </script>

it worked and showed ...

它工作并显示......

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

       ['aaa', 'bbb'],


];
</script>

回答by just.another.programmer

You don't want to call JavaScriptStringEncode on the entire string, that will also encode your literal indicators (which are being converted to \u0027 in your example). Instead, call it on each item in your array like this:

您不想在整个字符串上调用 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>

回答by Sean

I believe you could do most of the heavy lifting in .net and leverage Html.Raw to transform the object for you:

我相信您可以在 .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>

Since it's touched on in this question, HttpUtility.JavaScriptStringEncode() comes in really handy for strings containing newline characters:

由于在这个问题中涉及到它, 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>