[英]What is the correct way to create dynamic javascript in ASP.net MVC2?
I'm creating a Google Maps partial view/user control in my project that is passed a strongly typed list of objects containing latitude and longitude values. 我正在我的项目中创建Google Maps部分视图/用户控件,该控件传递了包含纬度和经度值的对象的强类型列表。
Currently, this is the code I have for the partial: 目前,这是我的部分代码:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Project.Models.Entities.Location>>" %>
<!-- Place for google to put the map -->
<div id="report_map_canvas" style="width: 100%; height: 728px; margin-bottom: 2px;">
</div>
<script type='text/javascript'>
google.load("maps", "2");
$(document).ready(initializeMap);
function initializeMap() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('report_map_canvas'));
map.setCenter(new GLatLng(51.5, -0.1167), 2);
<% foreach (var item in Model) { %>
map.addOverlay(new GMarker(new GLatLng('<%= Html.Encode(item.latitude)%>','<%= Html.Encode(item.longitude)%>'),{ title: '<%= Html.Encode(String.Format("{0:F}",item.speed)) %> km/h '}));
<% } %>
map.setUIToDefault();
}
}
</script>
Is it right to dynamically create the javascript file this way by looping over the list and emitting javascript? 通过遍历列表并发出javascript来动态创建javascript文件是否正确?
Is there a better way to do it? 有更好的方法吗?
i don't prefer this way because if you have many items to add you will make a big document to load for the user. 我不喜欢这种方式,因为如果要添加的项目很多,将为用户加载一个大文档。 instead I prefer to load the items as JSON and then iterate them and use google functions, eg:
相反,我更喜欢将项目加载为JSON,然后对其进行迭代并使用google函数,例如:
var data = [<%="{x:50.44444,y:30.44444,speed:50},..." %>] // generate you JSON Here as array
for(var i = 0; i < data.length; i++){
map.addOverlay(new GMarker(new GLatLng(data[i].y,data[i].x),{ title: data[i].speed + 'Km/h'}));
}
so you will not repeat this part 所以你不会重复这部分
map.addOverlay(new GMarker(new GLatLng(,),{ title: + 'Km/h'}));
which will increase the document size and page load 这将增加文档大小和页面加载
It's about as pretty as it gets. 差不多就可以了。
Slightly better way IMO would be to do something like this: IMO将采取以下更好的方法:
var locations = <%= Html.ToJson(Model) %>
at the top, and then your JavaScript would be pure JavaScript without embedded C# stuff. 在顶部,然后您的JavaScript将是没有嵌入式C#内容的纯JavaScript。
PS: You would need to extend HtmlHelper
with a ToJson
method in order to do this. PS:您需要使用
ToJson
方法来扩展HtmlHelper
来执行此操作。
ended up mixing both answers of Kronass and mookid. 最终混合了Kronass和mookid的答案。
Here is the final code for reference: 这是供参考的最终代码:
public static class MvcViewExtensions
{
public static string ToJson(this System.Web.Mvc.HtmlHelper helper, object obj) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
}
And the javascript in the view: 以及视图中的javascript:
<script type='text/javascript'>
google.load("maps", "2");
$(document).ready(initializeReportMap);
function initializeReportMap() {
if (GBrowserIsCompatible()) {
// Convert the reports to JSON
var reports = <%= Html.ToJson(Model) %>
var map = new GMap2(document.getElementById('report_map_canvas'));
map.setCenter(new GLatLng(51.5, -0.1167), 3);
for(var i = 0; i < reports.length; i++){
map.addOverlay(new GMarker(new GLatLng(reports[i].latitude,reports[i].longitude),{ title: reports[i].speed + 'Km/h'}));
}
map.setUIToDefault();
}
}
</script>
用页面的渲染来预加载数据没有什么错(实际上,这是您在循环中所做的事情,也许您可以在页面中输出json并编写一些javascript进行评估(这将减少重复字符,例如map .addOverlay ...)您只需要从模型中查找不受约束的输出,就不会希望以这种方式呈现1 000 000条记录
Take a look at this project ( Google map control for aspnet mvc ). 看一下这个项目( aspnet mvc的Google地图控件 )。 Maybe it can help you to create some helper that wrap the functionality you want.
也许它可以帮助您创建一些包装所需功能的帮助程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.