javascript - 数据不是来自 JsonResult Action 到 Javascript 脚本

标签 javascript c# asp.net model-view-controller

正如标题所示,我无法在 JavaScript 中获取和使用数据库中的数据。我有一个 Controller ,其中有我的数据库上下文构造函数和 JsonResult 操作,其中的信息按其应有的方式出现,但是当我尝试在 javascript 中使用数据时,我相信它为空。 这是我的 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);
        }
    }
}

这是我的 .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&#160;km (208&#160;mi) ' +
                    'south west of the nearest large town, Alice Springs; 450&#160;km ' +
                    '(280&#160;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>

这是我的模型:

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; }
    }
}

知道为什么数据没有传递给 JavaScript 吗? 任何帮助将不胜感激。

最佳答案

我认为您需要添加 JsonRequestBehavior.AllowGet 作为 Json() 方法中的第二个参数。

return Json(data, JsonRequestBehavior.AllowGet)

关于javascript - 数据不是来自 JsonResult Action 到 Javascript 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867522/

相关文章:

javascript - 在空闲/不活动 60 秒后重定向用户?

javascript - 在 Python 中模仿 JavaScript 数组

javascript - 如何使用带符号的 lastIndexOf() 函数

javascript - 如何在JavaScript中获取具有特定属性的所有子元素

c# - 正则表达式上的多行

c# - 为什么我的 C# winforms 应用程序中的第一个 HttpClient.PostAsync 调用非常慢?

c# - 将两个日期时间之间的差异转换为 HH :MM:SS format in asp.net C# 数据绑定(bind)类

asp.net - 从查询字符串中获取非英文字符

c# - VarBinary 到图像 url

c# - CORS 请求的资源上不存在 'Access-Control-Allow-Origin' header