C# 如何从MVC模型填充Google地图注释
我正在使用谷歌地图取得良好进展 下面的示例使用硬编码的JSON数组创建注释C# 如何从MVC模型填充Google地图注释,c#,json,google-maps,model-view-controller,C#,Json,Google Maps,Model View Controller,我正在使用谷歌地图取得良好进展 下面的示例使用硬编码的JSON数组创建注释 // You can make markers different colors... google it up! marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png') // a sample list of JSON encoded data of places to visit in Liverpool, UK // yo
// You can make markers different colors... google it up!
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png')
// a sample list of JSON encoded data of places to visit in Liverpool, UK
// you can either make up a JSON list server side, or call it from a controller using JSONResult
var data = [
{ "Id": 1, "PlaceName": "Liverpool Museum", "OpeningHours":"9-5, M-F","GeoLong": "53.410146", "GeoLat": "-2.979919" },
{ "Id": 2, "PlaceName": "Merseyside Maritime Museum ", "OpeningHours": "9-1,2-5, M-F", "GeoLong": "53.401217", "GeoLat": "-2.993052" },
{ "Id": 3, "PlaceName": "Walker Art Gallery", "OpeningHours": "9-7, M-F", "GeoLong": "53.409839", "GeoLat": "-2.979447" },
{ "Id": 4, "PlaceName": "National Conservation Centre", "OpeningHours": "10-6, M-F", "GeoLong": "53.407511", "GeoLat": "-2.984683" }
];
// Using the JQuery "each" selector to iterate through the JSON list and drop marker pins
$.each(data, function (i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.GeoLong, item.GeoLat),
'map': map,
'title': item.PlaceName
});
然而,我对MVC还是新手。请澄清评论中提到的两种方法?我有C#对象列表中的所有点。此列表位于页面的“我的模型”对象中。AlertsDashboardModel,其中包含一个列表-警报
我可能会直接在模型中使用列表,但我很好奇另一种方法如何工作
我正在使用MVC5和Razor
Paul第二种方法是指在页面加载后加载数据(或由于用户在页面上的交互,如按下按钮),然后向服务器发送ajax请求,服务器返回数据: 客户:
$(function () {
//fetch data points from server on page load
$.get("/YourController/GetDataPoints", function(data) {
$.each(data, function (i, item) {
//rest omitted
});
}
服务器:(在您的控制器中)
更新
如果只想在服务器端以javascript形式嵌入列表的第一种方法,请在razor视图中执行以下操作:
<script>
var data = @Html.Raw(JsonConvert.SerializeObject(Model.DataPoints));
</script>
var data=@Html.Raw(JsonConvert.SerializeObject(Model.DataPoints));
关于安全性您需要确保该列表中的所有内容都来自可信的来源,并且没有未经验证的字符串可以被注入页面(如果您的数据点类只包含int
您将很好)感谢这一点,我只需要添加JSONRequestBehavior。你能帮我做第一步吗?即服务器端列表。我希望能够在模型上绑定到我的属性,但是看起来好像没那么简单!我还想知道这种方式是否是一种好的方式,因为它的安全方面请看
<script>
var data = @Html.Raw(JsonConvert.SerializeObject(Model.DataPoints));
</script>