Javascript 如何从数据库中提取C#值以在JS中使用?

Javascript 如何从数据库中提取C#值以在JS中使用?,javascript,c#,asp.net,ajax,asp.net-mvc,Javascript,C#,Asp.net,Ajax,Asp.net Mvc,是否可以将一个值作为参数从C#方法传递给JavaScript GoogleMapsJavaScriptAPI想要传递硬编码的纬度和经度。是否可以用C#方法中的变量替换lat和lng 我在控制器上使用以下方法,从表中获取lat和lng。我在示例中连接了,但现在这并不重要 更新: 我的JSON信息如下所示: { "results": [ { "address_components": [ { "long_nam

是否可以将一个值作为参数从C#方法传递给JavaScript

GoogleMapsJavaScriptAPI想要传递硬编码的纬度和经度。是否可以用C#方法中的变量替换lat和lng

我在控制器上使用以下方法,从表中获取lat和lng。我在示例中连接了,但现在这并不重要

更新:

我的JSON信息如下所示:

{
   "results": [
      {
         "address_components": [
            {
               "long_name": "313",
               "short_name": "313",
               "types": [
                  "street_number"
               ]
            },
            {
               "long_name": "North Plankinton Avenue",
               "short_name": "N Plankinton Ave",
               "types": [
                  "route"
               ]
            },
            {
               "long_name": "Menomonee Valley",
               "short_name": "Menomonee Valley",
               "types": [
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name": "Milwaukee",
               "short_name": "Milwaukee",
               "types": [
                  "locality",
                  "political"
               ]
            },
            {
               "long_name": "Milwaukee County",
               "short_name": "Milwaukee County",
               "types": [
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name": "Wisconsin",
               "short_name": "WI",
               "types": [
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name": "United States",
               "short_name": "US",
               "types": [
                  "country",
                  "political"
               ]
            },
            {
               "long_name": "53203",
               "short_name": "53203",
               "types": [
                  "postal_code"
               ]
            }
         ],
         "formatted_address": "313 N Plankinton Ave, Milwaukee, WI 53203, USA",
         "geometry": {
            "location": {
               "lat": 43.0341871,
               "lng": -87.9119293
            },
            "location_type": "ROOFTOP",
            "viewport": {
               "northeast": {
                  "lat": 43.03553608029149,
                  "lng": -87.9105803197085
               },
               "southwest": {
                  "lat": 43.0328381197085,
                  "lng": -87.91327828029151
               }
            }
         },
         "place_id": "ChIJp3lqJaIZBYgRNWrX62hk4GU",
         "plus_code": {
            "compound_code": "23MQ+M6 Milwaukee, Wisconsin, United States",
            "global_code": "86MJ23MQ+M6"
         },
         "types": [
            "street_address"
         ]
      }
   ],
   "status": "OK"
}
因此,我将通过以下方法获得经度和纬度:

geoInfo.results[0].geometry.location.lat.ToString()


geoInfo.results[0].geometry.location.lng.ToString()

我看不出AddEmployee函数在做什么,但你所问的是可能的。你到底从一个雇员那里回来了什么?您的
success
回调没有用于查看其输出的参数。我正在本地工作,因此url更像是:``url:,```这只是一个示例。老实说,我不知道AJAX在做什么,我相信我会成功地运行这个方法:CallGoogleMapsJS(observer);包含整个observer记录的CallGoogleMapsJS()方法考虑到您的json结果,您可以通过results.geometry.location.lat和results.geometry.location.lng:)访问lat lng,但是通过Google Maps JS调用它时会是什么样子呢?var usermap=new google.maps.Map(document.getElementById('direction-Map'),{zoom:6,//用户的lat和lng中心:{lat:results.geometry.location.lat,lng:results.geometry.location.lng});
public async Task GetLatLongGeo(Observer observer) // Pass entire Observer record as a parameter
        {
        string address = (observer.StreetAddress); *// Get StreetAddress property from user's record*
        var Geokey = APIKey.GoogleGeocodekey; // Grab Google API key for Geocoding
        string url = $"https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={Geokey}";
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        string jsonresult = await response.Content.ReadAsStringAsync();
        if (response.IsSuccessStatusCode)
        {
                GeoCodeObject geoInfo = JsonConvert.DeserializeObject<GeoCodeObject>(jsonresult);
                var userLatitude = geoInfo.results[0].geometry.location.lat.ToString(); // Pulls lat
                var userLongitude = geoInfo.results[0].geometry.location.lng.ToString(); // Pulls lng
                StringBuilder latLongString = new StringBuilder();
                latLongString.Append(userLatitude + "," + userLongitude);
                string fullLatLong = latLongString.ToString();
                observer.LatLng = fullLatLong;
            }
        }
$.ajax({  
        url: 'EmployeeService.asmx/AddEmployee',  
        method: 'post',  
        data:   '{emp: ' + JSON.stringify(employee) + '}',  
        contentType: "application/json; charset=utf-8",  
        dataType: "json",  
        success: function () {  
            getAllEmployees();  
            console.log(data);  
        },  
        error: function (err) {  
            console.log(err);  
        }  
    });
{
   "results": [
      {
         "address_components": [
            {
               "long_name": "313",
               "short_name": "313",
               "types": [
                  "street_number"
               ]
            },
            {
               "long_name": "North Plankinton Avenue",
               "short_name": "N Plankinton Ave",
               "types": [
                  "route"
               ]
            },
            {
               "long_name": "Menomonee Valley",
               "short_name": "Menomonee Valley",
               "types": [
                  "neighborhood",
                  "political"
               ]
            },
            {
               "long_name": "Milwaukee",
               "short_name": "Milwaukee",
               "types": [
                  "locality",
                  "political"
               ]
            },
            {
               "long_name": "Milwaukee County",
               "short_name": "Milwaukee County",
               "types": [
                  "administrative_area_level_2",
                  "political"
               ]
            },
            {
               "long_name": "Wisconsin",
               "short_name": "WI",
               "types": [
                  "administrative_area_level_1",
                  "political"
               ]
            },
            {
               "long_name": "United States",
               "short_name": "US",
               "types": [
                  "country",
                  "political"
               ]
            },
            {
               "long_name": "53203",
               "short_name": "53203",
               "types": [
                  "postal_code"
               ]
            }
         ],
         "formatted_address": "313 N Plankinton Ave, Milwaukee, WI 53203, USA",
         "geometry": {
            "location": {
               "lat": 43.0341871,
               "lng": -87.9119293
            },
            "location_type": "ROOFTOP",
            "viewport": {
               "northeast": {
                  "lat": 43.03553608029149,
                  "lng": -87.9105803197085
               },
               "southwest": {
                  "lat": 43.0328381197085,
                  "lng": -87.91327828029151
               }
            }
         },
         "place_id": "ChIJp3lqJaIZBYgRNWrX62hk4GU",
         "plus_code": {
            "compound_code": "23MQ+M6 Milwaukee, Wisconsin, United States",
            "global_code": "86MJ23MQ+M6"
         },
         "types": [
            "street_address"
         ]
      }
   ],
   "status": "OK"
}