簡體   English   中英

數據不是從JsonResult Action到Javascript腳本

[英]Data not coming from JsonResult Action to Javascript Script

如標題所述,我無法從JavaScript中的數據庫中獲取和使用數據。 我有一個控制器,其中有我的數據庫上下文構造函數和一個JsonResult動作,其中的信息應以其應有的方式提供,但是當我嘗試在javascript中使用數據時,我認為它為null。 這是我的控制器:

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嗎? 任何幫助將不勝感激。

我認為您需要在Json()方法中添加JsonRequestBehavior.AllowGet作為第二個參數。

return Json(data, JsonRequestBehavior.AllowGet)

解決。 問題是return Json(data)返回一個字符串,類似{name: "x"}而我試圖將其作為data.Name並且我應該像data.name一樣完成它,因為它來自json 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM