Web API complex parameter properties are all null

前端 未结 18 1500
长情又很酷
长情又很酷 2020-11-30 04:18

I have a Web API service call that updates a user\'s preferences. Unfortunately when I call this POST method from a jQuery ajax call, the request parameter object\'s proper

18条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 04:59

    Using this technique posted by @blorkfish worked great:

    public HttpResponseMessage Post(Object model)
        {
            var jsonString = model.ToString();
            PreferenceRequest result = JsonConvert.DeserializeObject(jsonString);
        }
    

    I had a parameter object, which had a couple of native types and a couple more objects as properties. The objects had constructors marked internal and the JsonConvert.DeserializedObject call on the jsonString gave the error:

    Unable to find a constructor to use for type ChildObjectB. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

    I changed the constructors to public and it is now populating the parameter object when I replace the Object model parameter with the real object. Thanks!

提交回复
热议问题