简体   繁体   English

在ASP.NET MVC中使用地理编码,并希望将经纬度从JS传输到控制器中的某些函数

[英]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.

相关问题 使用ajax post将数据从JS传输到asp.net mvc中Controller中的函数。 但观点没有改变 - Using ajax post to transfer data from JS to a function in Controller in asp.net mvc. But the view is not changed 使用 SQL 数据库和 Leaflet Map 在 Z9E0DA8438E1E38B81CDD3 - Display Marker Using Latitude And Longitude From SQL Database With Leaflet Map In ASP.NET CORE MVC 如何使用ASP.Net MVC将JS文件中的javascript函数调用到控制器? - How to call javascript function in a JS file to a controller using ASP.Net MVC? ASP.net MVC 5已从控制器调用js函数且已返回 - ASP.net MVC 5 call a js function from controller with a return already 将数据从ASp.NET MVC Controller传递到JavaScript函数 - Pass data from ASp.NET MVC Controller to JavaScript function 如何在ASP.NET MVC3中使用jQuery函数将值从视图传递到控制器 - How to pass values from view to controller using jquery function in asp.net mvc3 从JS错误控制器中的ASP Net MVC调用函数 - ASP Net MVC calling function in controller from JS errors 使用JS和ASP.NET MVC的Web网格搜索功能 - Web grid search function using JS and ASP.NET MVC ASP.NET中的Javascript Geolocation纬度和经度变量访问 - Javascript Geolocation latitude and longitude variable access in asp.net 从ASP:.NET MVC控制器传输数据以查看 - Transfer Data from ASP:NET MVC Controller to view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM