数据不是从JsonResult Action到Javascript脚本

如标题所示,我无法从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++) {

    

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


三国纷争
浏览 267回答 2
2回答

汪汪一只猫

我认为您需要JsonRequestBehavior.AllowGet在Json()方法中添加第二个参数。return&nbsp;Json(data,&nbsp;JsonRequestBehavior.AllowGet)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript