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

解决方法 1:

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

<script type="text/javascript">
  
//some javascrpt code here to display map etc
  
  
//now add markers
 @foreach (var item in Model) {
    <text>
      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);
      });
  
   </text>
      }
</script>

 

posted @ 2018-09-12 15:14  唔愛吃蘋果  阅读(387)  评论(0编辑  收藏  举报