我在标记的 javascript 中使用此数组来显示 map 中的所有标记,因为静态工作是完美的
var markers = [
{
"title": 'Aksa Beach',
"lat": '31.6227697895779',
"lng": '-4.998779296875',
"description": '/www.google.com">Read more</a>'
}];
但是如果我可以在 html View 中添加模型中的一些标记就是我的问题
@foreach (var item in Model) {
markers.push({
"title": item.name,
"lat": item.lat,
"lng": item.long,
"description": item.descript
});
}
最佳答案
用单引号或双引号将值括起来。也自 item
是一个C#变量,需要使用@
。您还需要使用<text>
混合 C# 代码和纯文本(js 代码)时使用标签
<script>
var markers = [];
@foreach (var item in Model) {
<text>
markers.push({ "title": "@item.name",
"lat": "@item.lat",
"lng": "@item.long",
"description":"@item.descript"
});
</text>
}
</script>
关于javascript - 如何从 C# 更新 javascript 变量数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773245/