[英]Create JavaScript element within razor foreach loop
I'm adding the results of a search to a map using OpenStreetMap
. 我正在使用
OpenStreetMap
将搜索结果添加到地图中。 For each returned search result, I need to plot the latitude and longitude on a map as such: 对于每个返回的搜索结果,我需要这样在地图上绘制纬度和经度:
L.marker([39.616886,-86.310997]).addTo(map)
.bindPopup('the institutions name')
.openPopup();
How do I do that within a forEach
loop? 如何在
forEach
循环中执行此操作? Here's what I attempted, but I'm getting an error: 这是我尝试的操作,但出现错误:
The name L doesn't exist in the current context.
名称L在当前上下文中不存在。
@foreach (var u in Model.listschools)
{
L.marker([@u.lat, @u.longt]).addTo(map)
.bindPopup(@u.instnm)
.openPopup();
}
Razor is considering the code inside @foreach
as C# code, so you need to tell it that it is not. Razor正在将
@foreach
内的代码视为C#代码,因此您需要告诉它不是。 One way to do so can be by placing it between <text></text>
: 一种方法是将其放在
<text></text>
:
@foreach (var u in Model.listschools)
{
<text>L.marker([@u.lat, @u.longt]).addTo(map)
.bindPopup('@u.instnm')
.openPopup();</text>
}
Also notice that you forgot the quotes on this line: 另请注意,您忘记了此行上的引号:
.bindPopup('@u.instnm')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.