我有一个控制器定义为:
[AcceptVerbs(HttpVerbs.Post)] public JsonResult PostMoreData(DataContracts.Address address,DataContracts.GeoLocation geoLocation) { return Json("test"); } where DataContracts.Address and DataContracts.GeoLocation are complex types.
从我的视图我试图使用jQuery发布如下:
function PostMoreData() { var JsonAddress = { "Building": $('Building').val(),"UnitNumber": $('UnitNumber').val(),"StreetNumber": $('StreetNumber').val(),"StreetName": $('StreetName').val(),"StreetType": $('StreetType').val(),"Suburb": $('Suburb').val(),"State": $('State').val(),"Postcode": $('Postcode').val(),"MonthsAtAddress": $('MonthsAtAddress').val() }; var JsonGeoLocation = { "Latitude": $('Latitude').val(),"Longitude": $('Longitude').val() }; jQuery.post("/AddressValidation/PostMoreData",{address: JsonAddress,geoLocation: JsonGeoLocation},function(data,textStatus) { if (textStatus == "success") { var result = eval(data); if (result.length > 0) { alert(result); } } },"json"); }
但是,在控制器上,我得到空值.
它可以工作,如果我的控制器只需要一个参数,我只发布一个对象.
[AcceptVerbs(HttpVerbs.Post)] public JsonResult PostMoreData(DataContracts.Address address) { return Json("test"); }
function PostMoreData() { var JsonAddress = { "Building": $('Building').val(),"MonthsAtAddress": $('MonthsAtAddress').val() }; jQuery.post("/AddressValidation/PostMoreData",JsonAddress,"json"); }
任何想法如何我可以发布多个对象?