zoukankan      html  css  js  c++  java
  • asp.net mvc页面javascript代码中如何使用razor

    我们需要用<text>将javascript代码包含起来,强制让razor编译器回到内容模式,

    或者将javascript代码放在函数中,让razor编译器可以识别,请看下面两个例子:

    <script type="text/javascript">
     
    //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>

    例子2:

    <script type="text/javascript">
     
    //some javascript code here to display map etc
    ...
    //declare addMarker function
    function addMarker(latitude, longitude, title, description)
    {
          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);
          });
    }
     
    //now add markers
     @foreach (var item in Model) {
         @:addMarker(@item.Latitude, @item.Longitude, '@item.Title', '@item.Description');
     }
    </script>
  • 相关阅读:
    css 水平垂直居中总结
    计算机网络之应用层详解
    WPF 中 InitializeComponent 不存在解决方案
    [翻译]lithium 快速上手(QuickStart)
    [翻译]lithium 安装
    [翻译]lithium介绍
    [模板]离散化
    [总结]中位数及带权中位数问题
    [总结]Floyd算法及其应用
    [模板]SPFA判负环
  • 原文地址:https://www.cnblogs.com/superfeeling/p/5549367.html
Copyright © 2011-2022 走看看