我的JavaScript代码存在问题。
$.getJSON("/Home/GetJson", function (data) {
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(
item.CoordinatesPlace.Latitude,
item.CoordinatesPlace.Longitude
),
'map': map,
'title': item.Name
});
var infowindow = new google.maps.InfoWindow({
content:'@Html.ActionLink("Details","Details",new { id= item.Id})'
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
})
});
在'@Html.ActionLink("Details","Details",new { id= item.Id})'
我得“项目在当前上下文中不存在”。
如何将item.Id
传递给@Html.ActionLink
?
答案 0 :(得分:1)
content:'@Html.ActionLink("Details","Details",new { id= '+item.Id+'})'
我相信你得到了我在这里所做的改变。要将id的实际值附加到字符串,我们在这里使用'+'符号。
答案 1 :(得分:1)
你不能在剃刀中使用javascript变量,因为剃刀视图在服务器中被解析而javascript在客户端中运行。但是,您可以使用razor生成除参数之外的url,然后可以在javascript中添加:
content: '@Url.Action("Details", "Details")' + '?' + $.param({id: item.Id})
我在这里使用了 $。param ,因为如果你想添加几个参数,它会处理'&'他们之间,即$.param({id:1, otherParam:2}) -> id=1&otherParam=2