将列表传递给Ajax

时间:2014-07-22 18:40:56

标签: ajax asp.net-mvc

public List<double> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            foreach (var result in googleResults.results)
            {

                cordinates.Add(result.geometry.location.lng);

                cordinates.Add(result.geometry.location.lat);

            }

            return cordinates;

        }

现在,我希望两个人使用我的Ajax列表中的值:

 $(function() {
                    $('#ajax').click(function() {
                        $.ajax({

                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(item[0], item.GeoLat[1]), <-- I thought Item would contain my list and I could use its index
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

我是Ajax的初学者我真的很感激如果有人有空的话,会逐步解释它是如何工作的。谢谢!

Updat: 新课程:

public class LongLatModel
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

控制器:

public LongLatModel GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";

            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

            var cordinates = new List<double>();

            var longlat = new LongLatModel();


            foreach (var result in googleResults.results)
            {

                longlat.Latitude = result.geometry.location.lng;

                longlat.Longitude = result.geometry.location.lat;

            }

            return longlat;

        }

的Ajax:

$(function() {
                    $('#ajax').click(function() {
                        $.ajax({
                                // in a real scenario, use data-attrs on the html
                                // rather than linking directly to the url
                                url: '@Url.Action("GoogleGeoCode", "Home")',
                                context: document.body
                            })
                            .done(function(serverdata) {
                                data = serverdata;
                                $.each(data, function(i, item) {
                                    var marker = new google.maps.Marker({
                                        'position': new google.maps.LatLng(data.longitude, data.latitude),
                                        'map': map,
                                        'title': item.PlaceName,
                                        'Catch': item.Catch

                                    });
                                });
                            });
                    });

编辑:

好的,所以我设法将正确的信息输入AJAX(我认为......) 但是,我很难重新考虑如何将这两个值放在它们应该的位置。

enter image description here

我如何循环throgh并将它们放在正确的位置?

$。each(data,function(i,item){

var marker = new google.maps.Marker({  'position':新的google.maps.LatLng(LONG,LAT),

});

1 个答案:

答案 0 :(得分:2)

  1. 在C#方法中,您应该为LatLog创建数据结构,而不是返回List&lt; double&gt;在方法中。

    class LatLongModel
    {
        public double Latitude {get;set;}
        public double Longitude{get;set;}
    }
    
  2. 在JavaScript中,在done方法中,您可以检查:

    data.Longitude; data.Latitude;
    
  3. 如果请求失败,您还应该在Ajax请求中添加失败。请查看以下代码段:http://jsfiddle.net/JhWt4/
  4. <强>更新

    在C#代码中,您要创建并返回List

        public class GeoCodeResultModel
        {
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string PlaceName { get; set; }
            public string Catch { get; set; }
        }
    
        public List<GeoCodeResultModel> GoogleGeoCode(string address)
        {
            address = "Stockholm";
            string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    
            dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();
    
            var locations = new List<GeoCodeResultModel>();
    
            foreach (var result in googleResults.results)
            {
                locations.Add(new GeoCodeResultModel
                {
                     Longitude = result.geometry.location.lng,
                     Latitude = result.geometry.location.lat,
                     PlaceName = 'TODO',
                     Catch = 'TODO'
                }
            }
    
            return locations;
    
        }
    

    对于JavaScript代码:

    $.each(data, function(i, item) {
        var marker = new google.maps.Marker({
                     'position': new google.maps.LatLng(item.Latitude, item.Longitude), 
                     'map': map,
                     'title': item.PlaceName,
                     'Catch': item.Catch
        });
    });
    

    免责声明:这不是经过测试的代码,而是指向正确的方向。