ASP.NET WebAPI 2 deserializing JSON sets some nested objects to null

主宰稳场 提交于 2019-12-07 17:44:15

问题


I am encountering an issue while sending some JSON from an AngularJS application to a ASP.Net WebAPI 2 back-end. What happens is that some properties from the incoming request are set to null during deserialization.

Part of the request where the deserialization bug occurs:

12: {row: 9, column: 1, order: 13,…}
column: 1
domainEntityProperty: {$id: "157", id: 2616,…}
order: 13
renderType: {$id: "39", id: 1, class: "input"}
row: 9

Above snippet is piece of the request that is being sent. It's a so called screen property being saved. The request has an array of these objects. The strange thing is that everything is deserialized just fine when a maximum of one new screen property is added per request. When more than one is added, the domainEntityProperty and renderType properties are set to null on deserializing.

This is the model to which the JSON is converted in the controller:

public class ScreenPropertyModel
{
    public int Id { get; set; }

    public RenderTypeModel RenderType { get; set; }

    public int Order { get; set; }

    public int Row { get; set; }

    public int Column { get; set; }

    public DomainPropertyModel DomainEntityProperty { get; set; }
}

An example of the object after deserializing:

ScreenPropertyModel
-------------------
 - Column = 1,
 - Row = 9,
 - Order = 13,
 - DomainEntityProperty = null,
 - RenderType = null

Does anybody know why the DomainEntityProperty and RenderType properties are null? And more specifically: why does this only happen when multiple new objects are being saved?

I'd be thankful if anyone could point me in the right direction here.

Update ## - DomainProperty and RenderType definitions

DomainProperty:

public class DomainPropertyModel
{
    public int Id { get; set; }

    // some more simple properties

    public IList<DefaultValueModel> DefaultValues { get; set; }

    public IList<ScreenPropertyModel> ScreenProperties { get; set; }

    public ICollection<BusinessRuleProperty> BusinessRuleProperties { get; set; }
}

RenderType:

public class RenderTypeModel
{
    public int Id { get; set; }

    public string Class { get; set; }
}

As far as I can see in Chrome developer tools, all these properties are present on the object that is being sent to the server.

来源:https://stackoverflow.com/questions/30977999/asp-net-webapi-2-deserializing-json-sets-some-nested-objects-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!