Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetJsonData()
{
var data = new List<threat>();
data.Add(new threat { category_id = 0, threat_id = 0, lat = 12, lng = 12 });
data.Add(new threat { category_id = 0, threat_id = 0, lat = 13, lng = 13 });
data.Add(new threat { category_id = 0, threat_id = 0, lat = 14, lng = 14 });
data.Add(new threat { category_id = 0, threat_id = 0, lat = 15, lng = 15 });
return Json(data, JsonRequestBehavior.AllowGet);
}
}
Ausblick:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var locations = [];
$.getJSON("/Home/GetJsonData", null, function (data) {
locations = data;
});
$("#btnShowData").click(function() {
for(var i =0;i < locations.length;i++){
var location = locations[i];
var message = "Location Lat - " + location.lat + ".Location Lon - " + location.lng;
alert(message);
}
});
});
</script>
<div>
<input type="button" id="btnShowData" value="Show Data" />
</div>
Ich verwende eine statische List<T>
in dem obigen Beispiel, also ersetzen Sie einfach diese Logik, um Daten aus Ihrer Datenbank zurückgeben und der Rest wird wie gewohnt arbeiten.Nicht sicher, was ORM Sie verwenden, aber hier ist eine einfache