Google Geocoding Result into MVC Controller
I am utilizing the JavaScript API v3. I am basically geocoding an address as follows:
geocoder.geocode({ 'address': address }, function (results, status) {
//Get results array here
}
This is working successfully. I now need to pass that JSON to an MVC controller. I have seen numerous ways to do this, but I cannot get it working with the Geocode result.
From Haack: (I have a collection on objects that duplicate the structure of the result. At the outermost object being a result[]
(see below)).
geocoder.geocode({ 'address': address }, function (results, status) {
var jsonT = JSON.stringify(results);
$.ajax({
url: '/Ctrl/Action',
type: "POST",
dataType: 'json',
data: jsonT,
contentType: "application/json; charset=utf-8",
success: function (result) {
alert(result.Result);
}
});
}
The controller method fires, however the value is always null
.
[HttpPost]
public ActionResult Action(GoogleGeoCodeResponse geoResponse)
{
//geoResponse is always null
return View();
}
My Google Class(s)
[Serializable]
public class GoogleGeoCodeResponse
{
//public string status { get; set; }
public results[] results { get; set; }
}
[Serializable]
public class results
{
public string[] types { get; set; }
public string formatted_address { get; set; }
public address_component[] address_components { get; set; }
public geometry geometry { get; set; }
public string partial_match { get; set; }
}
[Serializable]
public class address_component
{
public string[] types { get; set; }
public string long_name { get; set; }
public string short_name { get; set; }
}
[Serializable]
public class geometry
{
public location location { get; set; }
public string location_type { get; set; }
public viewport viewport { get; set; }
public bounds bounds { get; set; }
}
[Serializable]
public class location
{
public string lat { get; set; }
public string lng { get; set; }
}
[Serializable]
public class viewport
{
public southwest southwest { get; set; }
public northeast northeast { get; set; }
}
[Serializable]
public class bounds
{
public southwest south开发者_StackOverflow中文版west { get; set; }
public northeast northeast { get; set; }
}
[Serializable]
public class southwest
{
public string lat { get; set; }
public string lng { get; set; }
}
[Serializable]
public class northeast
{
public string lat { get; set; }
public string lng { get; set; }
}
Assuming you have added a JSON value provider factory in Application_Start
:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
and the actual JSON request matches your view model signature:
{ geoResponse: {
results: [
{ types: [ 't1', 't2' ], formatted_address: 'abc', ... },
{ types: [ 't3', 't4' ], formatted_address: 'def', ... },
...
] }
}
you should successfully get the view model in the controller action. Also you probably don't need to decorate your models with the [Serializable]
as that's not used by XML and JSON serializers. It's used for binary serialization.
After tearing my hair out, I took a break and came back to find the issue right away with the help of @Darin Dimitrov help. The problem was that the JSON was not matching the object. The GoogleGeoCodeResponse
object has an array of results
called results, however the JSON returned from Google has an array of results but it is not named. Adding this before my stringify
adds the proper naming structures and all bound well.
var o = { results: results };
精彩评论