[英]Data not coming from JsonResult Action to Javascript Script
As the title says, i am not able to get and use data from Database in my javascript. 如标题所述,我无法从JavaScript中的数据库中获取和使用数据。 I have a Controller in which i have my Database Context Constructor and a JsonResult Action, in which the information comes as it should, but when i'm trying to use data in the javascript it comes as null i believe.
我有一个控制器,其中有我的数据库上下文构造函数和一个JsonResult动作,其中的信息应以其应有的方式提供,但是当我尝试在javascript中使用数据时,我认为它为null。 Here is my Controller:
这是我的控制器:
namespace ProiectColectiv.Controllers
{
public class AdminMapController : Controller
{
private readonly ProiectColectivContext _context;
public AdminMapController(ProiectColectivContext context)
{
_context = context;
}
public ActionResult AdminMap(User user)
{
return View("~/Views/Home/AdminMap.cshtml", user);
}
public JsonResult GetAllLocation()
{
var data = _context.Parkings.ToList();
return Json(data);
}
}
}
Here is my .cshtml with the javascript: 这是我的.cshtml和javascript:
@*@model ProiectColectiv.Models.Parking*@
@{
ViewData["Title"] = "Admin Map";
}
<br/>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 800px;
width: 1200px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 46.770920, lng: 23.589920},
zoom: 13
});
$.get("@Url.Action("GetAllLocation","AdminMap")", function (data, status) {
var marker = [];
var contentString = [];
var infowindow = [];
for (var i = 0; i < data.length; i++) {
marker[i] = new google.maps.Marker({ position: { lat: parseFloat(data[i].Latitudine), lng: parseFloat(data[i].Longitudine) }, map: map });
contentString[i] = '<div id="content">' +
'<div id="siteNotice">' +
'</div>' +
'<h1 id="firstHeading" class="firstHeading">'+ data[i].Name+'</h1>' +
'<div id="bodyContent">' +
'<p><b>' + data[i].Name +'</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the ' +
'Northern Territory, central Australia. It lies 335 km (208 mi) ' +
'south west of the nearest large town, Alice Springs; 450 km ' +
'(280 mi) by road. Kata Tjuta and Uluru are the two major ' +
'features of the Uluru - Kata Tjuta National Park. Uluru is ';
infowindow[i] = new google.maps.InfoWindow({
content: contentString[i]
});
//marker[i].addListener('click', function() {
// infowindow[i].open(map, marker[i]);
//});
}
})
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9HfqjqR0VDGD5N1sCd_nU8qC7J5SXATM&callback=initMap"
async defer></script>
</body>
</html>
And here is my Model: 这是我的模型:
namespace ProiectColectiv.Models
{
public class Parking
{
public int Id { get; set; }
public string Name { get; set; }
public double Longitudine { get; set; }
public double Latitudine { get; set; }
public int NrFastChargingSpots { get; set; }
public int NrNormalChargingSpots { get; set; }
public List<Station> Stations { get; set; }
}
}
Any idea why the data is not passed to javascript? 知道为什么数据不传递给javascript吗? Any help will greatly appreciated.
任何帮助将不胜感激。
我认为您需要在Json()
方法中添加JsonRequestBehavior.AllowGet
作为第二个参数。
return Json(data, JsonRequestBehavior.AllowGet)
Resolved. 解决。 The problem was that
return Json(data)
was returning a string, something like {name: "x"}
and i was trying to take it as data.Name
and i should have done it like data.name
, as it comes from json. 问题是
return Json(data)
返回一个字符串,类似{name: "x"}
而我试图将其作为data.Name
并且我应该像data.name
一样完成它,因为它来自json 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.