- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如标题所示,我无法在 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 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>
这是我的模型:
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/
我已经在 Controller 上设置了这个测试方法,以消除它的任何复杂性。基于我从搜索中发现的所有结果,这应该有效。我不确定我在这里遗漏了什么。 public JsonResult test() {
据我所知,我收到解析器错误,因为我返回的某些数据包含撇号。 我收到错误: SyntaxError: Unexpected end of input at Object.parse (native) a
我在 Controller 中使用以下操作,并且尝试在 jq.grid 中显示房间数量; public JsonResult Incercare() { DBContext
我需要像这样从我的 ASP.NET MVC 3 应用程序返回一个 JSON 结果: { 0: value1, 1: value2 } 但是我不能创建一个匿名类型的对象,它将数字作为字段名称。那我该怎么
我对此还是新手,所以我会尝试解释我在做什么。基本上我想要的是根据前一个下拉列表的值加载一个下拉列表,并且我希望它加载数据并在另一个列表更改时出现。这是我在 Controller 中编写的代码: pub
试图显示来自 Controller 的成功消息? [HttpPost] public JsonResult SuccesMsg() { return Json(ne
我正在尝试构建一个 Web 服务来与需要安全连接的移动应用程序进行交互。 通过不安全的连接,我从服务器获得了所需的 JSON 结果,并可以构建适当的 JSON 对象(在测试设备上)。 所需的 JSON
我使用的是 MVC 3 并且有一个操作返回具有 187 个名称值对的 JsonResult(作为 List ),但是从 ajax 调用接收到的数据通常被截断并且无法解析。 这总是通过 JsonResu
在我的客户端,我有一个如下所示的 ajax 调用: $.ajax({ url: "Controller/ListResult",
我如何在 C# asp.net MVC 中返回 json 结果中的字符串列表? 我有这个 Controller public JsonResult AutoCompletePart(string
我试图在我的一个 Controller 中对返回 JsonResult 的方法进行单元测试。令我惊讶的是,以下代码不起作用: [HttpPost] public JsonResult Test() {
我有一个运行良好的 JsonResult,并从一些 POCO 返回 JSON。我想将 JSON 作为字符串保存在数据库中。 public JsonResult GetJSON() { Json
我有应该返回 json 字符串的 MVC Controller 方法。 public JsonResult myMethod() { .... return Json(new { succes
在我的 HomeController 中,我有以下方法: public JsonResult AjaxTest(Position postData) { Session["lat"]
尝试构建一个 mvc 项目,它将上传的图像从数据库加载到轮播中。我遇到的问题是,图像将创建一个 byte[] 列表并作为 JsonResult 从 Controller 发送到 View 。问题是我不
以下查询成功运行。 var tabs = ( from r in db.TabMasters orderby r.colID
我创建了一个 ActionFilterAttribute,用于在允许处理 MVC Controller get/post 请求之前验证帐户是否可用于处理。如果该帐户不可用,我想向客户返回错误。如果请求
我对此已经摸不着头脑有一段时间了,并决定让 SO 社区尝试一下...... 我有一些操作可以响应客户端的 POST,执行简单的任务并返回 JsonResult如果成功,则从具有简单 bool “成功”
使用此脚本,我从 JsonResult (GetDevicesTable) 获取数据并将它们放入表中 ( id="OrderDevices") $(document).ready(function
我正在尝试使用 jquery.Ajax 将数据发布到返回 JsonResult 的 ASP.NET MVC2 操作方法。一切都很好,除了当响应返回浏览器时,它被视为文件下载而不是传递到成功处理程序中。
我是一名优秀的程序员,十分优秀!