说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

asp.net mvc页面javascript代码中如何使用razor

HTML文档下载 WORD文档下载 PDF文档下载
razor虽然方便,但有时候配合其它内容时也需要很多技巧,让razor引擎可以识别。

 

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

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

 

例子1:

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

 

备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘