或者将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>
附例二:
<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')
}
通过request的getParameter的方法获取1、通过request.getParameter("参数名")获取后台的值
2、通过<%=变量%>的方式赋给js变量
示例:
<script>
<%
String s2 = (String)request.getParameter("参数名")//获取后台参数给s2变量,<%%>表示这范围内是服务器解析的语言
%>
var s = '<%=s2%>'//输出s2给js变量s
</script>
利用Ajax调用控制器中的方法来使用1、定义变量值 var xx=2
2、 $.ajax({
url: "控制器/方法",
data: { site_id: xx },
cache: false,
success: function (data) {
},
error: function () {
alert("error")
}
})