mvc2 html.textbox from model, dynamically update as marker dragged before saving
I am trying to populate <%: Html.TextBoxFor(model => model.FaveRunLatLng1)%>
dynamically as a user drags a marker around a map.
I need to use this value as the map is initialized with the FaveRunLatLng1
value. However, when the user drags the marker, this needs to be updated so that when the Save
button is hit, the most current LatLng
is saved. The code for the marker is in Javascript, Google maps API v3.
Should I be using something like:
<div class="editor-label">
<%: Html.TextBoxFor(model => model.FaveRunLatLng1, new {@class = "coords"})%>
</div>
This is the google maps listener for the drag event:
google.maps.event.addListener(marker, 'drag', function () {
var point = marker.getPosition();
var lat = point.lat();
var lng = point.lng();
coordStr = lat.toStri开发者_JAVA技巧ng() + ", " + lng.toString();
document.getElementById("newCoords").value = coordStr;
map.setCenter(point);
});
the variable newCoords
acts as a test to ensure the dragged marker is updating as it is dragged.
Yes you should do it that way. But instead of using test newCoords
, just use the element ID that got created by the HtmlHelper
which is FaveRunLatLng1
:
document.getElementById("FaveRunLatLng1").value = coordStr;
This means you'd be accessing your textbox by ID rather than CSS class. I suppose you've provided class name coords
for the purpose of accessing your textbox?
精彩评论