分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

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

发布时间:2023-09-06 01:54责任编辑:熊小新关键词: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
<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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved