如何在razor中创建JavaScript字符串 [英] How to create a javascript string in razor
问题描述
我已经看到了有关该主题的一些帖子和一些博客,但似乎都没有提到我得到的输出.
I have seen some posts regarding this topic and a few blogs, but none seem to mention the output I'm getting.
我想要的是生成一个包含其信息的google maps地图.手动输入信息会得到正确的信息.这样就可以了.
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.
要卡住的地方是当我要动态创建带有字符串的javascript数组,并在地图上提供所需的信息.
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.
我要获取的html代码是:
The html code I want to get is:
<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数组:
Very simple javascript array, which I thought to create with:
<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>
转义单引号不起作用. 我在做什么错了?
Escaping the single quotes doesn't work. What am I doing wrong?
推荐答案
只需尝试
<script type="text/javascript">
var projects = [
@Html.Raw("['" + "aaa" + "', '" + "bbb" + "'],")
];
</script>
它工作并显示...
<script type="text/javascript">
var projects = [
['aaa', 'bbb'],
];
</script>
这篇关于如何在razor中创建JavaScript字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!