【发布时间】:2015-11-04 11:36:49
【问题描述】:
我在 ASP.Net MVC 中有一个如下所示的表单。它带有一个 get ,但在我提交之前我想运行一些 pre get 处理(从谷歌地图获取地理坐标),然后如果坐标返回我将提交给我的控制器。如果没有坐标返回,那么我会显示一些错误消息并且不会点击控制器。
这是我的表格
@using (Html.BeginForm("Search", "Home", FormMethod.Get))
{
<div class="form-group">
@Html.TextBoxFor(model => model.SearchQuery, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.SearchQuery, "", new { @class = "text-danger" })
@Html.TextBoxFor(model => model.ClassDate, "{0:MM/dd/yyyy}", new { @class = "datefield form-control" })
@Html.ValidationMessageFor(model => model.ClassDate, "", new { @class = "text-danger" })
</div>
<button type="submit" id="submitSearch" class="btn btn-default">Search</button>
}
这是我的 javascript 事件,它将获取地址并将其提交到谷歌地图。 我尝试在开头添加一个 event.preventDefault 以阻止它调用表单“GET”,但这没有用。
$("#submitSearch").click(function (event) {
event.preventDefault();
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('SearchQuery').value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
if (results[0].formatted_address) {
region = results[0].formatted_address + '<br/>';
}
var infowindow = new google.maps.InfoWindow({
content: 'Location info:<br/>Country Name:' + region +
'<br/>LatLng:' + results[0].geometry.location + ''
});
google.maps.event.addListener(marker, 'click', function () {
// Calling the open method of the infoWindow
infowindow.open(map, marker);
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});
我应该从我的页面中删除表单并使用 JQuery 和 Ajax 调用它吗?
【问题讨论】:
标签: javascript jquery ajax asp.net-mvc forms