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

     

  • 相关阅读:
    php安全编程&python测试实例编写
    MySQL注入技巧性研究
    第一届“百度杯”信息安全攻防总决赛
    不想在315“中奖”?你得躲过这些坑!
    这些故事你尽管听,不奇葩算我输!
    str2-045漏洞事件,你想要的这里都有
    python多线程在渗透测试中的应用
    【ZCTF】easy reverse 详解
    UVA
    用Thinphp发送电子邮件的方法
  • 原文地址:https://www.cnblogs.com/shirly1981/p/5732683.html
Copyright © 2020-2023  润新知