![](/img/trans.png)
[英]Using ajax post to transfer data from JS to a function in Controller in asp.net mvc. But the view is not changed
[英]Using geocoding in asp.net mvc and want to transfer latitude and longitude from JS to some function in controller
更新。
我嘗試了你說的話。 功能是。
[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();
}
但是知道它沒有顯示出新的觀點。 我不知道為什么? 仍然顯示舊視圖。 它不會重定向到新視圖。 新視圖未顯示。 它仍然保持相同的觀點。
////////////
我正在使用Google地圖的地理編碼功能在asp.net mvc應用程序的Razor視圖中訪問位置的緯度和經度。
我想將緯度和經度傳遞給控制器中的函數。 但是什么也沒發生。 以下是我的代碼段。
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
});
控制器:
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);
}
似乎缺少了一些東西,首先是您要發布的操作上的[HttpPost]屬性,然后是正確數量的參數,看起來像您傳入2但期望3? 請參見下面的工作示例。
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>
控制器動作
[HttpPost]
public ActionResult populate_place(string lati, string longi)
{
return RedirectToAction("About", "Home");
}
如果在控制器操作上設置了斷點,則在檢查參數時會返回它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.