Search code examples
c#asp.netjsondeserializationjson-deserialization

ASP.Net Controller improperly Deserializing nested list of objects


I have the following C# type definition:

public class GraphicSubmission
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Byline { get; set; }
    public string BylineTitleId { get; set; }
    public string BylineTitleDescription { get; set; }
    public string Category { get; set; }

    public List<GraphicBinary> GraphicBinaries;

    public GraphicSubmission()
    {
        GraphicBinaries = new List<GraphicBinary>();
    }
}

which includes a nested List of these objects:

public class GraphicBinary
{
    public SubmittedImageType ImageType { get; set; }
    public string OriginalFilename { get; set; }
    public string ItemId { get; set; }
    public string RecordId { get; set; }
    public int RecordSequenceNumber { get; set; }
}

I have the following Controller method which takes as one of its arguments a GraphicSubmission object:

[HttpPost]
public JsonResult Graphic(PhotoAction actionType, bool hid, GraphicSubmission graphicModel)

When I call the controller method from an AngularJS site with the following JSON in the body of an HTTP POST:

{
    "Title": "AP Poll Stay at Home Protest Approval",
    "Description": "A new UChicago Divinity School/AP-NORC poll finds that two-thirds of Democrats and about half of Republicans disapprove of recent protests of stay at home orders.;",
    "Byline": "F. Strauss",
    "BylineTitleDescription": "STAFF",
    "BylineTitleId": "STF",
    "Category": "a",
    "GraphicBinaries": [
        {
            "ImageType": "PrintGraphicAI",
            "ItemId": "dd142b48fe7c4cc6bf9b42c9c9402e7d",
            "RecordId": "dd142b48fe7c4cc6bf9b42c9c9402e7d",
            "RecordSequenceNumber": 0,
            "OriginalFilename": "ChicagoShootings.ai"
        },
        {
            "ImageType": "PrintGraphicJPEG",
            "ItemId": "ccce25ddc1cb45d898b09eb0d91fcecc",
            "RecordId": "ccce25ddc1cb45d898b09eb0d91fcecc",
            "RecordSequenceNumber": 0,
            "OriginalFilename": "ChicagoShootings.jpg"
        }
    ]
}

The method is invoked properly but the GraphicBinaries field of the GraphicSubmission object is always empty.

Given the JSON I would expect it to contain 2 entries.

My question is what do I need to do to get ASP.Net to deserialize the nested list of objects properly?

I have looked at some related articles here such as:

Deserialize JSON array(or list) in C#

Passing JSON Object and List of Objects to ASP.Net Controller [duplicate]

Post JSON array to mvc controller

but none of them seem to have the key to figuring this issue out.


Solution

  • As the comments point out the GraphicBinaries field in the GraphicSubmission class needs to be made a property by adding { get; set; }.

    Here's an article that points out the distinction:

    What is the difference between a field and a property?