如何从控制器传递对象列表以在JavaScript中查看和使用它

时间:2018-11-01 07:02:36

标签: javascript c# asp.net-mvc web foreach

这是我的控制器:

public ActionResult Index()
{
    // marker list is a list of marrker class
    ViewBag.MarkerList = Utility.markerList;
    return View();
}

这是我的JavaScript:

<script>

        var mymap = L.map('mapid', { doubleClickZoom: false }).setView([35.701060, 51.378075], 11.5);

        L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
            maxZoom: 18,
            id: 'mapbox.streets',
            accessToken: 'pk.eyJ1IjoiYXJtNG44IiwiYSI6ImNqbml0cnY5dzBtbncza280ZGNlNjZ6b2wifQ.ZZBr5TriuKvyev4Dh8bUpg'
        }).addTo(mymap);

        function myFunction(item) {

            var marker = L.marker([item.lat, item.lng], { riseOnHover: true }).addTo(mymap);
            marker.bindPopup("I am a popup.<br>"+ item.lat + "," + item.lng);
        }

        var array = @ViewBag.MarkerList;

        array.foreach(myFunction);

    </script>

我想在JavaScript列表中使用foreach来调用其中的函数。我想在页面加载中运行此代码。

0 个答案:

没有答案