[英]Using geocoding in asp.net mvc and want to transfer latitude and longitude from JS to some function in controller
UPDATED. 更新。
I tried what you said. 我尝试了你说的话。 The function is.
功能是。
[HttpPost]
public ActionResult populate_place(string lati, string longi)
{
list_placesModels list_place = new list_placesModels();
list_place.Latitude = lati;
list_place.Longitude = longi;
return View();
}
But know its is not showing the new view. 但是知道它没有显示出新的观点。 I don't know why ?
我不知道为什么? Still the old view is shown.
仍然显示旧视图。 It is not redirecting to new view.
它不会重定向到新视图。 The new view is not shown.
新视图未显示。 It still remains on the same view.
它仍然保持相同的观点。
//////////// ////////////
I am using Geo-coding feature of Google maps to access latitude and longitude of a location in my Razor view in asp.net mvc application. 我正在使用Google地图的地理编码功能在asp.net mvc应用程序的Razor视图中访问位置的纬度和经度。
I want to pass that Latitude and longitude to a function in controller. 我想将纬度和经度传递给控制器中的函数。 But nothing is happening.
但是什么也没发生。 Below is my code snippet.
以下是我的代码段。
JS code: JS代码:
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker( {
map: map,
[0].geometry.location
});
// I want to pass these value of latitude and longitude a function
// list_places() in UserController.
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
$.post("/User/list_places",
{
lati: latitude,
longi : longitude
});
Controller: 控制器:
public ActionResult list_places(string usernameid, string lati, string longi) {
list_placesModels list_place = new list_placesModels();
list_place.Latitude = lati;
list_place.Longitude = longi;
return View(list_place);
}
It looks like a few things are missing, firstly a [HttpPost] attribute on the action you are posting too, then the correct number of arguments, it look slike your passing in 2 but expecting 3? 似乎缺少了一些东西,首先是您要发布的操作上的[HttpPost]属性,然后是正确数量的参数,看起来像您传入2但期望3? See below for a working example.
请参见下面的工作示例。
Html/Javascript HTML / Javascript
<script type="text/javascript">
function buttonclick() {
var requestData = {
lati: 'latitude',
longi: 'longitude'
};
$.ajax({
url: '/Home/populate_place',
type: 'POST',
data: requestData,
dataType: 'json'
});
};
</script>
<button onclick="buttonclick()">Click</button>
Controller Action 控制器动作
[HttpPost]
public ActionResult populate_place(string lati, string longi)
{
return RedirectToAction("About", "Home");
}
If you put a breakpoint on the controller actions return if you inspect the parameters you'll see they are populated. 如果在控制器操作上设置了断点,则在检查参数时会返回它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.