简体   繁体   English

如何从C#更新javascript变量数组

[英]how to update javascript variable array from c#

am working with this array in javascript of marks to show all marks in the map as static work is perfect 正在使用javascript标记中的此数组来显示地图中的所有标记,因为静态工作是完美的

 var markers = [
{
    "title": 'Aksa Beach',
    "lat": '31.6227697895779',
    "lng": '-4.998779296875',
    "description": '/www.google.com">Read more</a>'
}];

but if i can add some marks from model in html view is my problem 但是如果我可以在html视图中从模型中添加一些标记是我的问题

@foreach (var item in Model) {
    markers.push({
    "title": item.name,
    "lat": item.lat,
    "lng": item.long,
    "description": item.descript
});
}

Wrap the values with single or double quotes. 用单引号或双引号引起来的值。 Also since item is a C# variable, you need to use @ . 另外,由于item是C#变量,因此您需要使用@ You also need to use <text> tag as you are mixing C# code and plain text (js code) 在混合C#代码和纯文本(js代码)时,还需要使用<text>标签。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM