Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MVC ActionLink参数“;不';“t存在于当前上下文中”;_Javascript_Jquery_Json - Fatal编程技术网

Javascript MVC ActionLink参数“;不';“t存在于当前上下文中”;

Javascript MVC ActionLink参数“;不';“t存在于当前上下文中”;,javascript,jquery,json,Javascript,Jquery,Json,我对JavaScript代码有问题 $.getJSON("/Home/GetJson", function (data) { $.each(data, function (i, item) { var marker = new google.maps.Marker({ 'position': new google.maps.LatLng( item.CoordinatesPlace.Latit

我对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(“详细信息”,“详细信息”,新的{id=item.id})
中,我得到“当前上下文中不存在项”

如何将
item.Id
传递到
@Html.ActionLink

content:'@Html.ActionLink("Details","Details",new { id= '+item.Id+'})'

我相信你得到了我在这里所做的改变。要将id的实际值附加到字符串中,我们在这里使用“+”符号。

您不能在razor中使用javascript变量,因为razor视图在服务器中解析,javascript在客户端运行。但是,您可以使用razor生成url,但参数除外,然后可以添加到javascript中:

content: '@Url.Action("Details", "Details")' + '?' + $.param({id: item.Id}) 

我在这里使用了$.param,因为如果您想添加几个参数,它将为您处理在它们之间放置'&',即,
$.param({id:1,otherParam:2})->id=1&otherParam=2

,看起来不像JavaScript。这是什么样的标记?它是服务器端处理的吗?我想你应该添加“C#”和“ASP.NET MVC”标记,因为它不是纯JavaScription,我应该添加什么样的标记?它是MVC项目的一部分{id='+item.id+'}给了我一个错误“文字字符太多”:/非常感谢!但是我怎么能从@Url.Action(“Details”,“Details”)'+'?'+$.param({id:item.id})?@myID33创建一个链接呢?我不知道我是否理解你的意思。。。操作将返回一个Url,例如
http://localhost/Details/Details
然后将其与?id=1连接,这将产生一个有效的url:
http://localhost/Details/Details?id=1
。如果您在锚定标记(
)中添加了锚定标记,当您单击它时,它将导致使用id=1调用Details的Controller action detail。我的意思是相同的,但不是正确的form@myID33将url存储在javascript变量中,然后使用jquery设置锚标记的href,下面是一个非常简单的例子。您的建议不起作用的原因是href不需要javascript表达式。