I'm developing a web page that display open street map and gets data from SQL server database by long and lat coordinates
i use asp.net mvc and give me this wrong
Uncaught TypeError: Cannot read property 'Name' of undefined
where i bind data from database by javascript
Model
this model data i create GetMap() that return data from database by Json function
[HttpPost]
public JsonResult GetMap()
{
var data1 =(from p in db.Map
select new
{
Name = p.Name,
Latitude = p.Latitude,
Logitude = p.Logitude,
Location = p.Location,
Description = p.Description,
Id = p.Id
}).ToList().Select(res => new Map
{
Name = res.Name,
Latitude = res.Latitude,
Logitude = res.Logitude,
Location = res.Location,
Description = res.Description,
Id = res.Id
});
return Json(data1, JsonRequestBehavior.AllowGet);
}
</pre>
view file
view file that display map and return data by Json function
<div id="mapid" style="height:600px"></div>
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
var map = L.map('mapid').setView([31.291340, 34.244190], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
$.ajax({
type: "POST",
url: '/Maps/GetMap',
success: function (data) {
var result = JSON.stringify(data);
for (var i = 0; i < result.length; ++i) {
var popup =
'<b>Name:</b> ' + data[i].Name +
'<br/><b>Latitude:</b> ' + data[i].Latitude +
'<br/><b>Longitude:</b> ' + data[i].Logitude +
'<br/><b>Location:</b> ' + data[i].Location;
L.marker([data[i].Latitude, data[i].Logitude])
.bindPopup(popup)
.addTo(map);
}
},
error: function (xhr) {
console.log(xhr.responseText);
alert("Error has occurred..");
}
});
});
</script>
result.length
butresult
should bedata
. The error message indicates that somedata[i]
is undefined. – JamesSelect
part after the.ToList()
. And, if those are the only properties in theMap
object, you might as well get rid of the entire query and just doreturn Json(db.Map.ToList())
– adiga