关于什么是错,有些失落了。我在C#中有一个列表,试图在cshtml文件中的foreach循环中进行迭代。我的循环代码是:
debugger;
@foreach(CheckInWeb.Models.Geo.cGeoPoint item in ViewBag.Geometries)
{
<text>
var wkt = @item.Geometry;
var feature = wktReader.readFeature(wkt);
feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
feature.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 1.0, scale: 0.5, src: '@Url.Content("~/images/Icons/marker.png")'
})
}));
featureCollection.push(feature);
</text>
}
浏览器中显示的行为var wkt = @item.Geometry;
,浏览器控制台窗口中的错误为:Uncaught SyntaxError: missing ) after argument list
浏览器中呈现的脚本如下:
debugger;
var wkt = POINT (69.059 34.5411);
var feature = wktReader.readFeature(wkt);
feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
feature.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 1.0, scale: 0.5, src: '/images/Icons/marker.png'
})
}));
featureCollection.push(feature);
由于@item.Geometry
是一个字符串,因此我尝试将输入的变量包装在单引号和双引号中,并且保持不变。
有什么想法吗?
答案 0 :(得分:0)
****最终解决方案*****
正如Stephen所建议的那样,首先转换为数组确实确实使它更容易:
var geometried = @Html.Raw(Json.Serialize(ViewBag.Geometries));
for (var i = 0; i < geometried.length; i++) {
var feature = wktReader.readFeature(geometried[i].geometry);
feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
feature.setStyle(new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 0.75, scale: 0.25, src: '@Url.Content("~/images/Icons/marker.png")'
})
}));
featureCollection.push(feature);
}