如何从控制器传递viewdata来查看javascript

时间:2017-09-16 18:11:56

标签: c#

我在控制器中有一个viewdata如下

 namespace GeoPortal.Controllers
 {
public class FarmsController : Controller
{
    GeoPortalEntities db = new GeoPortalEntities();
    // GET: Farms
    public ActionResult Index()
    {
        var model = db.Farms;

        var mylist = db.Farms.ToList();
        ViewData["Farms"] = mylist; // Send this list to the view

        return View(model.ToList());

    }



}
}

我想将此列表传递给我的视图并将其呈现为谷歌地图。我想要一个for循环语句,它将从传递给前端的viewdata中获取地址

   @using (Html.BeginForm())
  {
 <head>
<style>
    #map {
        height: 100px;
        width: 100%;
    }
</style>
 </head>
  <body>
<div id="map"></div>
<script>
  function initMap() {
    var uluru = {lat: -25.363, lng: 131.044};
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: uluru
    });
    var marker = new google.maps.Marker({
      position: uluru,
      map: map
    });
  }
</script>
<script async defer
        src="https://maps.googleapis.com/maps/api/js?
  key=AIzaSyDMg_EyG8kqEaPXS0Gn-67UzA4z7fpfBQg&callback=initMap">
</script>
 </body>
 }

1 个答案:

答案 0 :(得分:0)

您可以在<text>伪元素中执行逻辑。

检查此问题Using Razor within JavaScript