What is the correct way to create dynamic javascript in ASP.net MVC2?
Posted
by sabbour
on Stack Overflow
See other posts from Stack Overflow
or by sabbour
Published on 2010-05-09T09:01:27Z
Indexed on
2010/05/09
9:08 UTC
Read the original article
Hit count: 389
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.
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?
Is there a better way to do it?
© Stack Overflow or respective owner