인덱스 (프로젝트 홈페이지)에서 내 프로젝트의 페이지로 연결하려고합니다. 한 번만 작동했습니다.페이지가 MVC에서 한 번만 표시됩니다.
이 코드는 완벽하게 작동하고
public ActionResult TotalPs()
{
ViewBag.Message = "TotalPs";
var totalQuery =
(from lot in db.parkingLots
orderby lot.PricePerHour
select new
{
ID = lot.parkingLotID,
address = lot.addressParkingLot,
latitude = lot.latitudeParkingLot,
longtitude = lot.longtitudeParkingLot,
Status = lot.statusParkingLot,
PricePerHour = lot.PricePerHour
})
.Union(from pub in db.publicParkings
orderby pub.PricePerHourpublicParking
select new
{
ID = pub.publicParkingID,
address = pub.addressPublicParking,
latitude = pub.latitude,
longtitude = pub.longtitude,
Status = pub.statusParking,
PricePerHour = pub.PricePerHourpublicParking
});
var data2 = totalQuery.ToList();
var jsonString2 = JsonConvert.SerializeObject(data2);
if (jsonString2 != null)
{
if (!Directory.Exists(Server.MapPath("~/Content/")))
{
Directory.CreateDirectory(Server.MapPath("~/Content/"));
}
}
System.IO.File.WriteAllText(Server.MapPath("~/Content/TotalJson.json"), jsonString2);
db.SaveChanges();
return View();
}
이 코드는보기
@{
ViewBag.Title = "TotalPs";
}
<h2>TotalPs</h2>
<head>
<style>
#map {
height: 700px;
width: 1000px;
border: 1px solid black;
margin: 0 auto;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyApsEFrg9i2dlhq493ME30ETlDGDNbQvWI" type="text/javascript"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<br />
<br />
<div class="topnavI" align="center">
<p style="font-size:16px;"> Enter the address to search for available parking spaces</p>
<input type="text" placeholder="Search for address" size="40">
</div>
<br />
<br />
<div id="map"></div>
<script>
var map;
function initialize() {
var mapProp = {
center: new google.maps.LatLng(32.04772750000001, 34.7609645),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapProp);
};
$(document).ready(function() {
var url = "../../Content/TotalJson.json";
initialize();
$.getJSON(url, function (data) {
$.each(data, function (i, field) {
$('#list').append("<li>" + data[i].latitude + " & " + data[i].longtitude + "</li>");
createMarker(data);
function createMarker(data) {
var marker = new google.maps.Marker({
icon: 'https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png',
position: new google.maps.LatLng(data[i].latitude, data[i].longtitude),
map: map,
title: field.crossroad
});
};
});
});
});
</script>
<body>
</body>
그리고 서버 측
@Html.ActionLink("INBAL", "TotalPs", "parkingLots")
따라서 인덱스 페이지에서 인 parkingLotscontroller에서입니다 보기 immediatly 위로, 하지만 cliet 측면에 시간이 좀 걸립니다 로드 후 오류가 발생합니다. 무엇이 문제이며 어떻게 해결합니까? 감사합니다.
호기심에서 벗어난 이유는 단지 JsonResult를 반환하는 다른 컨트롤러 액션을 사용하는 대신 JSON을 파일에 작성하는 이유입니다. 여러 사용자가 문제가 될 수 있습니다. 또한'db.SaveChanges()'는 무엇을위한 것인가? –
db.SaveChanges()는 DB를 최신 상태로 유지하고이 영역에서 새로운 Json을 사용하므로 새로운 Contorller를 만드는 것이 문제를 해결해야한다고 생각합니까? – InbalK