ASP.Net控制器不正确地反序列化嵌套的对象列表

问题描述 投票:0回答:1

我具有以下C#类型定义:

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>();
        }
    }

其中包括这些对象的嵌套列表:

   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; }
    }

我有以下Controller方法,将GraphicSubmission对象作为其参数之一:

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

当我从AngularJS站点使用HTTP POST正文中的以下JSON调用控制器方法时:

{
    "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"
        }
    ]
}

该方法已正确调用,但是GraphicSubmission对象的GraphicBinaries字段始终为空。

鉴于JSON,我希望它包含2个条目。

我的问题是要使ASP.Net正确反序列化嵌套的对象列表我需要做什么?

我看过这里的一些相关文章,例如:

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

但是他们似乎都没有解决这个问题的钥匙。

c# asp.net json deserialization json-deserialization
1个回答
0
投票

正如评论所指出的那样,需要通过添加{get;来将GraphicSubmission类中的GraphicBinaries字段设为属性。组; }。

这里有一篇文章指出了区别:

What is the difference between a field and a property?

© www.soinside.com 2019 - 2024. All rights reserved.