Asp.net mvc怎么在razor里寫js代碼


我試圖在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
< script type = "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 ( 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM