【发布时间】:2021-09-23 13:22:31
【问题描述】:
@page
@model Vescoverer.Pages.Registration.LocationModel
@{
ViewData["Title"] = "Location";
}
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div id="long" class="form-group">
<label asp-for="User.Longitude" class="control-label"></label>
<input id="long" asp-for="User.Longitude" class="form-control" />
<span asp-validation-for="User.Longitude" class="text-danger"></span>
</div>
<div id="lat"class="form-group">
<label asp-for="User.Latitude" class="control-label"></label>
<input asp-for="User.Latitude" class="form-control" v />
<span asp-validation-for="User.Latitude" class="text-danger"></span>
</div>
<div class="form-group">
<input asp-for="User.Longitude" type="submit" value="Next" class="btn btn-primary" />
</div>
</form>
<button onclick="getLocation()">Get Location</button>
</div>
</div>
<script>
var lat = document.getElementById("lat");
var long = document.getElementById("long");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
lat.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
lat.innerHTML = position.coords.latitude;
long.innerHTML = position.coords.longitude;
$.ajax(
{
type: "POST", //HTTP POST Method
url: "Location",
data: { //Passing data
Longitude: //insert data value, //Reading text box values using Jquery
Latitude: //insert data value
}
});
}
</script>
我希望经度和纬度结果进入适当的表单输入值,以便在用户单击“获取位置”时传递到后端,而不是结果用 innerHTML 字符串替换表单值。我知道 asp-for 标记 heper 处理服务器端方面,但我不确定如何让 javascript 结果显示在表单值中。因此,当用户按下下一步时,数据将传递到数据库中,正如您所看到的,我也在尝试使用 Ajax,但我不太确定。
【问题讨论】:
标签: javascript asp.net-core razor-pages