首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从MVC模型中填充Google注释

如何从MVC模型中填充Google注释
EN

Stack Overflow用户
提问于 2015-01-08 01:36:13
回答 1查看 920关注 0票数 1

我正在取得很好的进展使用谷歌地图。

下面的示例使用硬编码的JSON数组创建注释

代码语言:javascript
运行
复制
// 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,它包含一个列表-警报

我可能会直接在模型中使用列表,但我想知道另一种方法是如何工作的。

我在Razor中使用MVC 5

保罗

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-08 01:45:56

第二种方法是在页面加载(或由于用户在页面上的交互(如按下按钮)后加载数据),然后将ajax请求发送给返回数据的服务器:

客户端:

代码语言:javascript
运行
复制
$(function () {
    //fetch data points from server on page load
    $.get("/YourController/GetDataPoints", function(data) {
        $.each(data, function (i, item) {
        //rest omitted
    });
}

服务器:(在Controller中)

代码语言:javascript
运行
复制
public JsonResult GetDataPoints()
{
    return new JsonResult { Data = ...your list here };
}

更新

如果您只想将列表作为javascript嵌入服务器端的第一种方法,请在您的剃须刀视图中这样做:

代码语言:javascript
运行
复制
<script>
        var data = @Html.Raw(JsonConvert.SerializeObject(Model.DataPoints));
</script>

关于安全,您需要确保列表中的所有内容都来自受信任的源,并且没有未经验证的字符串可以注入到页面中(如果数据点类只包含int,您就可以了)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27831646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档