0

我試圖用asp.net mvc製作一個網站4 & EF6我想從MSSQL表中將值傳遞給javascript。到目前爲止一切工作正常,除非我通過Latitude & Longitudegoogle-map-api函數,地圖不顯示在我的視圖。我使用Html.HiddenFor幫手來傳遞來自<span>的值。這裏是我的代碼,將MSSQL表值傳遞給javascript

控制器

public ActionResult BranchDetails(int BrId) 
    { 
     Branch branchDetails = abdb.Branches.Find(BrId); 
     return View(branchDetails); 
    } 

查看

<script> 
    function initialize() { 
     var marker; 
     var LatTag = document.getElementById("Lat"); 
     var Lat = LatTag.getElementsByTagName("span"); 
     var LngTag = document.getElementById("Lng"); 
     var Lng = LngTag.getElementsByTagName("span"); 
     var mapProp = { 
      center: new google.maps.LatLng(LatTag, LngTag), 
      zoom: 18, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 

     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(Lat, Lng), 
      animation: google.maps.Animation.BOUNCE 
     }); 

     marker.setMap(map); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

<body> 
    <div class="container well" style="min-width: 100%; padding-right: 5px;">   
     <div id="googleMap"></div> 
     <span id="Lat">@Html.HiddenFor(model => model.Latitude)</span> 
     <span id="Lng">@Html.HiddenFor(model => model.Longitude)</span> 
     <h4><b>@Html.DisplayFor(model => model.Title)</b></h4> 
     <p><strong>Address:</strong> @Html.DisplayFor(model => model.Address)</p> 
     <p><strong>Contact No. :</strong> @Html.DisplayFor(model => model.ContactNo)</p> 
     <p><strong>Contact Person(s):</strong> @Html.DisplayFor(model => model.ContactPerson)</p> 
    </div> 
</body> 

點要注意的是在MSSQL我Longitude & Latitude字段varchar模式設置。我不確定它是否會導致問題,但只是需要通知。我怎麼解決這個問題?糟糕的需要這個幫助。 TNX。

回答

0

沒關係,找到我自己的解決方案。這工作對我來說,而不是給予span id,我給Html.HiddenFor幫手的id。由於某種原因,JavaScript沒有選擇我猜測的跨度ID。這裏是我的代碼,

<script> 
    function initialize() { 
     var marker; 
     var Lat = parseFloat(document.getElementById("Lat").value); 
     var Lng = parseFloat(document.getElementById("Lng").value); 
     var mapProp = { 
      center: new google.maps.LatLng(Lat, Lng), 
      zoom: 18, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 

     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(Lat, Lng), 
      animation: google.maps.Animation.BOUNCE 
     }); 

     marker.setMap(map); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
<body> 
    <div id="googleMap"></div> 
    @Html.HiddenFor(model => model.Latitude, new { id = "Lat" }) 
    @Html.HiddenFor(model => model.Longitude, new { id = "Lng" }) 
</body>