gpt4 book ai didi

javascript - google.maps.InfoWindow 中的 MVC ActionLink

转载 作者:行者123 更新时间:2023-11-29 21:12:16 25 4
gpt4 key购买 nike

我对 JavaScript 代码有疑问

$.getJSON('@Url.Action("GetData", "Home")', function (data) {
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
'map': map,
'title': item.PlaceName
});

marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png')

var infowindow = new google.maps.InfoWindow({

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

});

如何使用 @Html.ActionLinkId 传递给 Controller ​​?

在 Controller 中,我有一个 POST 版本的 Index 方法。如何在此方法中使用 ActionLink 传递 Id?在 View 中,我尝试使用 ViewBag 但没有显示任何内容

public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult Index(int id)
{
ViewBag.id = id;
return RedirectToAction("Index", "Home");
}

编辑:我的 View 代码:

@{
ViewBag.Title = "Home Page";
}

Id : @ViewBag.id

<script src="http://maps.google.com/maps/api/js? key=**********" type="text/javascript"></script>

<style>
.stationInfo {
height: 150px;
width: 250px;
}
</style>

<div id="canvas" style="height: 600px; width:600px;"></div>


@section scripts {
<script type="text/javascript">

$(document).ready(function () {
GetMap();
});

function GetMap() {

google.maps.visualRefresh = true;

var Moscow = new google.maps.LatLng(55.752622, 37.617567);

var mapOptions = {
zoom: 15,
center: Moscow,
mapTypeId: google.maps.MapTypeId.G_NORMAL_MAP
};

var map = new google.maps.Map(document.getElementById("canvas"), mapOptions);

var myLatlng = new google.maps.LatLng(55.752622, 37.617567);

var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Name'
});

marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png')

$.getJSON('@Url.Action("GetData", "Home")', function (data) {керы
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
'map': map,
'title': item.PlaceName
});

marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png')

var infowindow = new google.maps.InfoWindow({

content:
'<form action="@Url.Action("Index", "Home")" method="POST">' +
'<input type="hidden" name="id"/>' +
'<a class="f" href="#" data-id="' + item.Id + '">Details</a></form>'
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
})
});
$(function () {
$(document).on("click", "a.f", function (e) {
e.preventDefault();
var v = $(this).data("id");
var f = $(this).closest("form");
f.find("[name='id']").val(v);
f.submit();
});
});

}
</script>
}

但是当我点击链接后重定向ViewBag为空(

最佳答案

您不能将参数从 javascript 传递到 razor。但是你可以尝试在他生成后将id注入(inject)链接:

var link = '@Html.Action("Details","Details")'+'/'+item.id;

var infowindow = new google.maps.InfoWindow({

content: '<a href="'+link+'">details</a>'

});

对于更新的问题尝试这样做:

[HttpPost]
public ActionResult Index(int id)
{
ViewBag.id = id;
return Index();
}

关于javascript - google.maps.InfoWindow 中的 MVC ActionLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com