Skip to main content
 首页 » 编程设计

c#之使用 Google Maps API 绑定(bind)到 .ADO 模型时出错

2024年01月01日40jyk

为什么会出现此错误?

Compiler Error Message: CS1061: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'lat' and no extension method 'lat' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

.Ado 模型
国际、纬度、经度、内容

Controller

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MapApp.Models; 
 
namespace MapApp.Controllers 
{ 
    [HandleError] 
    public class HomeController : Controller 
    { 
        mapEntities _db = new mapEntities(); 
 
        public ActionResult Index() 
        { 
            return View(_db.river); 
        } 
 
        public ActionResult About() 
        { 
            return View(); 
        } 
    } 
} 

查看

@model IEnumerable<MapApp.Models.river> 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2> Index</h2> 
<script type="text/javascript"> 
@foreach (var item in Model) 
{ 
    <text> 
      var markerlatLng = new google.maps.LatLng(@(Model.lat), @(Model.lng)); 
      var contents = '@(Model.contents)'; 
 
      var infowindow = new google.maps.InfoWindow({ 
          content: contents 
      }); 
 
      var marker = new google.maps.Marker({ 
          position: markerlatLng, 
          title: contents, 
          map: map, 
          draggable: false 
      }); 
 
      google.maps.event.addListener(marker, 'click', function () { 
          infowindow.open(map, marker); 
      }); 
 
   </text> 
} 
</script> 

请您参考如下方法:

Model 是 river 的可枚举。看起来你在河流中循环是正确的,但你在循环中错误地引用了每个项目。

尝试:

var markerlatLng = new google.maps.LatLng(@(item.lat), @(item.lng)); 
var contents = '@(item.contents)'; 

这应该可以解决问题。