来自模型的mvc2 html.textbox,在保存之前动态更新为标记拖动

时间:2011-09-26 11:07:37

标签: html asp.net-mvc-2 textbox

我正在尝试动态填充<%: Html.TextBoxFor(model => model.FaveRunLatLng1)%>,因为用户会在地图上拖动标记。

我需要使用此值,因为地图已使用FaveRunLatLng1值初始化。但是,当用户拖动标记时,需要对其进行更新,以便在点击Save按钮时保存最新的LatLng。标记的代码在Javascript,Google maps API v3。

我应该使用类似的东西:

<div class="editor-label">
   <%: Html.TextBoxFor(model => model.FaveRunLatLng1, new {@class = "coords"})%> 
</div>

这是用于拖动事件的Google地图监听器:

 google.maps.event.addListener(marker, 'drag', function () {
            var point = marker.getPosition(); 
            var lat = point.lat();
            var lng = point.lng();

            coordStr = lat.toString() + ", " + lng.toString();

            document.getElementById("newCoords").value = coordStr;
            map.setCenter(point);
        });

变量newCoords充当测试,以确保拖动的标记在拖动时更新。

1 个答案:

答案 0 :(得分:0)

是的,你应该这样做。但是,不要使用测试newCoords,只需使用由HtmlHelper FaveRunLatLng1创建的元素ID:

document.getElementById("FaveRunLatLng1").value = coordStr;

这意味着您将通过ID而不是CSS类访问文本框。我想您为了访问文本框而提供了班级名称coords