json - Google Geocoding Result into MVC Controller -
i utilizing javascript api v3. geocoding address follows:
geocoder.geocode({ 'address': address }, function (results, status) { //get results array here }
this working successfully. need pass json mvc controller. have seen numerous ways this, cannot working geocode result.
from haack: (i have collection on objects duplicate structure of result. @ outermost object being 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, value null
.
[httppost] public actionresult action(googlegeocoderesponse georesponse) { //georesponse 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 southwest { 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 have added json value provider factory in application_start
:
valueproviderfactories.factories.add(new jsonvalueproviderfactory());
and actual json request matches view model signature:
{ georesponse: { results: [ { types: [ 't1', 't2' ], formatted_address: 'abc', ... }, { types: [ 't3', 't4' ], formatted_address: 'def', ... }, ... ] } }
you should view model in controller action. don't need decorate models [serializable]
that's not used xml , json serializers. it's used binary serialization.
Comments
Post a Comment