首页 > Web开发 > 详细

Asp.net mvc怎么在razor里写js代码

时间:2018-05-13 13:25:45      阅读:173      评论:0      收藏:0      [点我收藏+]

我试图在Razor里写JS代码,但是不行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<scripttype="text/javascript">
  
//some javascrpt code here to display map etc
  
  
//now add markers
 @foreach (var item in Model) {
  
      var markerlatLng = new google.maps.LatLng(@(Model.Latitude), @(Model.Longitude));
      var title = ‘@(Model.Title)‘;
      var description = ‘@(Model.Description)‘;
      var contentString = ‘<h3>‘ + title + ‘</h3>‘ + ‘<p>‘ + description + ‘</p>‘
  
      var infowindow = new google.maps.InfoWindow({
          content: contentString
      });
  
      var marker = new google.maps.Marker({
          position: latLng,
          title: title,
          map: map,
          draggable: false
      });
  
      google.maps.event.addListener(marker, ‘click‘, function () {
          infowindow.open(map, marker);
      });
  
  
      }
</script>

 

解决方法 1:

使用<text>这个伪元素来强制Razor从编译模式返回到内容模式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<script type="text/javascript">
  
//some javascrpt code here to display map etc
  
  
//now add markers
 @foreach (varitem inModel) {
    <text>
      varmarkerlatLng = newgoogle.maps.LatLng(@(Model.Latitude), @(Model.Longitude));
      vartitle = ‘@(Model.Title)‘;
      vardescription = ‘@(Model.Description)‘;
      varcontentString = ‘<h3>‘+ title + ‘</h3>‘‘<p>‘+ description + ‘</p>‘
  
      varinfowindow = newgoogle.maps.InfoWindow({
          content: contentString
      });
  
      varmarker = newgoogle.maps.Marker({
          position: latLng,
          title: title,
          map: map,
          draggable:false
      });
  
      google.maps.event.addListener(marker,‘click‘,function() {
          infowindow.open(map, marker);
      });
  
   </text>
      }
</script>

Asp.net mvc怎么在razor里写js代码

原文:https://www.cnblogs.com/sjqq/p/9031523.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!