将列表项从c#传递给javascript数组
我有以下代码在gmap上显示多个标记
function init() { var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addListener(marker, 'click', (function (marker, i) { return function () { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } return false; }
我想让这个动态,所以我必须通过这个
var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ];
从c#代码到这个js。
我试过Hidden Field和这段代码
List oGeocodeList = new List { "'Bondi Beach', -33.890542, 151.274856, 4", "'Coogee Beach', -33.923036, 151.259052, 5", "'Cronulla Beach', -34.028249, 151.157507, 3", "'Manly Beach', -33.80010128657071, 151.28747820854187, 2", "'Maroubra Beach', -33.950198, 151.259302, 1" }; var geocodevalues = string.Join(",", oGeocodeList.ToArray()); ClientScript.RegisterArrayDeclaration("locations", geocodevalues);
但没有运气任何参考将对我有所帮助
使用Json.Net
var obj = new[] { new object[] { "Bondi Beach", -33.890542, 151.274856, 4 }, new object[] { "Coogee Beach", -33.923036, 151.259052, 5 }, new object[] { "Cronulla Beach", -34.028249, 151.157507, 3 }, new object[] { "Manly Beach", -33.80010128657071, 151.28747820854187, 2 }, new object[] { "Maroubra Beach", -33.950198, 151.259302, 1 }, }; var json = JsonConvert.SerializeObject(obj);
我认为你能做的就是创建一个List。
例:
List data = new List (); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return serializer.Serialize(data);
而已。
我希望它有效
基本上我认为你要做的是创建一个JSON
字符串。 JSON
允许您在多种语言(包括JavaScript和C#)之间以序列化字符串格式传递对象。 我建议看看JSON .NET库 。 它是一个非常棒的库,可以让您安全有效地将项目序列化为C#中的字符串。
我还建议你创建一个更多的OOP结构,而不是传递一个多维数组。 为此,您需要为您的位置创建一个类,我将假设以下内容:
public class Location { public string Name { get; set; } public double Lat { get; set; } public double Lng { get; set; } }
然后,您将构建一个List
,然后使用JSON .NET库对其进行序列化,这将非常简单:
List oGeocodeList = new List () { //... }; string json = JsonConvert.SerializeObject(oGeocodeList);
使用此JSON,您可能要将其写入隐藏字段或JavaScript中的变量。 这将允许您通过JavaScript在您的页面上引用它。 还有相当全面的文档 ,certificate非常有用!
然后可以在JavaScript中像任何其他js对象一样访问它,如下所示:
for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i].Lat, locations[i].Lng), map: map }); // ... }
我会使用Json.Net或C#Json编码器将C#编码为Json。 然后将其写入隐藏字段。 然后使用Json2在JavaScript端解码它
你必须使用这样的东西:在你的控制器中:
var locations = [ ['Bondi Beach', -33.890542, 151.274856, 4], ['Coogee Beach', -33.923036, 151.259052, 5], ['Cronulla Beach', -34.028249, 151.157507, 3], ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], ['Maroubra Beach', -33.950198, 151.259302, 1] ]; var locationsString = JsonConvert.SerializeObject(locations); ViewBag.locationsString = locationsString; return View();
在你的java脚本中使用它像这样:
var data = JSON.parse(@ViewBag.locationsString);
你可以将你的数据放在数组中并在javascript中使用它的每个函数:
$.each(data, function (i, item) { //enter your code }
对于javascript而不是jquery使用:
for (var i = 0; i < data.length; i++) { addMarker(data[i], map); }
- 如何在使用AsyncFileUpload时在clientside中重新命名serveride
- 在C#.Net中调用MongoDB存储的Javascript函数
- 在ajax post问题中传递数据
- 正则表达式为字母数字密码,至少包含1个数字和字符
- 如何在c#中检索字符串formData js
- C#WebBrowser控件 – 在AJAX之后获取文档元素?
- 有没有办法拦截和修改asp.net中的html输出流,结合javascript?
- 使用asp.net C#Javascript登录使用Finerprint的Asp.net Web应用程序
- 移动请求检测和重定向到Asp中的移动页面(Active Server Pages)