c# - Posting json to mvc 4 WebApi controller action. Why doesn't it deserialize my array? -
the following has been driving me bit crazy. have found couple of similar problems did not offer solution. trying post json object containing few data items. 1 of them list of object itself. here is:
{ "claimtype":"trade", "claimedproductid":"4", "claiminguserid":"2", "message":"test", "tradeoffers":[ { "offeredproductid":"7", "offeredquantity":"5" }, { "offeredproductid":"12", "offeredquantity":"2" } ] }
this json validates.
my controller looks this:
public class productcontroller : apicontroller { [httppost] public void claim(claimviewmodel claimviewmodel) { //do amazing stuff data viewmodel. //sorry guys. stuff tooo cool posted here see //not ;-) } }
the claimviewmodel posting looks this:
public class claimviewmodel { public claim.claimrequesttypes claimtype { get; set; } public int claimedproductid { get; set; } public int claiminguserid { get; set; } public string message { get; set; } public list<tradeoffer> tradeoffers { get; set; } }
for completeness sake, here's tradeoffer class:
public class tradeoffer { int offeredproductid { get; set; } int offeredquantity { get; set; } }
before staring posting javascript, use chrome restconsole test bad boy, can bit better under hood. make request following settings:
- its post request
- the body content type "application/json"
- i send json request payload in raw body
then here's happens: properties on claimviewmodel
deserialized nice , easy. however, tradeoffers
property instantiated , when debugging shows list count of 2 (so far good) values of objects in list (properties offeredproductid
, offeredquantity
) 0/zero (not null!)
properties in tradeoffer
class private (default access modifier in c#), makes impossible set them outside. try making them public
:
public class tradeoffer { public int offeredproductid { get; set; } public int offeredquantity { get; set; } }
Comments
Post a Comment