我试图在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>

Asp.net mvc怎么在razor里写js代码的更多相关文章

  1. ASP.NET MVC 4 中Razor 视图中JS无法调试

    解决方法 1.首先检查IE中这2个属性是否勾选了. 2.选择IE浏览器进行调试,调试方法有2种     A:采用debugger;的方法,如下图所示: 这时不用调试断点就会在debugger位置中命中 ...

  2. ASP.NET MVC 4 中Razor 视图中JS无法调试 (重要)

    谷歌浏览器,firefox,IE 都可以 1.首先检查IE中这2个属性是否勾选了. 2.选择IE浏览器进行调试,调试方法有2种 A:采用debugger;的方法,如下图所示: 这时不用调试断点就会在d ...

  3. (转)Asp.Net Mvc视图引擎Razor介绍

    Asp.Net Mvc视图引擎Razor介绍 1.Razor介绍 程序园原创,转载请注明:http://www.kwstu.com/ArticleView/dabaomvc_2014082408205 ...

  4. ASP.NET MVC应用程序把文字写在图片上

    原文:ASP.NET MVC应用程序把文字写在图片上 Insus.NET实现这篇<MVC把随机产生的字符串转换为图片>http://www.cnblogs.com/insus/p/3624 ...

  5. asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析

    下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...

  6. 转发-基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现

    基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现   概述 在日常开发中, 我们经常谈起模块化/插件化架构,这样可既可以提高开效率,又可以实现良好的扩展性,尤其对于产品化的系统有 ...

  7. 【问题】Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数

    [问题]Asp.net MVC 的cshtml页面中调用JS方法传递字符串变量参数. [解决]直接对变量加引号,如: <button onclick="deleteProduct('@ ...

  8. ASP.NET MVC 5 学习教程:生成的代码详解

    原文 ASP.NET MVC 5 学习教程:生成的代码详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...

  9. ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。

    一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript . 前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaS ...

随机推荐

  1. python matplotlib 画图

    import numpy as np import matplotlib.pyplot as plt from pylab import * numpy 常用来组织源数据: 使用 plot 函数直接绘 ...

  2. java基础篇---XML解析(一)

    XML是可扩展标记语言 在XML文件中由于更多的是描述信息的内容,所以在得到一个xml文档后应该利用程序安装其中元素的定义名称去除对应的内容,这样的操作称为XML解析. 在XML解析中W3C定义SAX ...

  3. Beaglebone Black教程Beaglebone Black中的Cloud9 IDE基本使用

    Beaglebone Black教程Beaglebone Black中的Cloud9 IDE基本使用 ​Beaglebone Black中的Cloud9 IDE基本使用 Cloud9是集成在Beagl ...

  4. Caused by: java.io.IOException: Added a key not lexically larger than previous.

    为了重复这个实验,遇到不少坑 https://www.iteblog.com/archives/1889.html /** * Created by Administrator on 2017/8/1 ...

  5. 【MySql 】is not allowed to connect to this MySql server 无法访问远程MySQL数据库

    问题:访问远程MySQL数据库时报错[is not allowed to connect to this MySql server ]不允许访问,因为MySQL默认只有本机localhost能够访问M ...

  6. WPF中自定义路由事件

    public class MyButtonSimple: Button { // Create a custom routed event by first registering a RoutedE ...

  7. Material Design 相关资源

    Materialpalette -- Material配色工具 Materialup -- Material设计灵感: Material Design 相关好文: <超全面总结!深聊MATERI ...

  8. 22 Best Sites To Download Free Sprites

    http://unity3diy.blogspot.com/2014/11/Free-Sprites-Download-For-YourGames.html ————————————————————— ...

  9. Javascript导航菜单13则

    来源:http://www.noupe.com/ajax/13-awesome-java-script-css-menu.html翻译:http://parandroid.com下面为你准备了13个利 ...

  10. MYSQL查询前30条数据

    , LIMIT 接受一个或两个数字参数. 参数必须是一个整数常量. 如果给定两个参数,第一个参数指定第一个返回记录行的偏移量, 第二个参数指定返回记录行的最大数目. 初始记录行的偏移量是 (而不是 )