ASP.Net 控制器不正确地反序列化嵌套对象列表
ASP.Net Controller improperly Deserializing nested list of objects
我有以下 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#
Post JSON array to mvc controller
但其中 none 似乎掌握了解决此问题的关键。
正如评论指出的那样,GraphicSubmission class 中的 GraphicBinaries 字段需要通过添加 { get; 成为 属性放; }.
这是一篇指出区别的文章:
What is the difference between a field and a property?
我有以下 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#
Post JSON array to mvc controller
但其中 none 似乎掌握了解决此问题的关键。
正如评论指出的那样,GraphicSubmission class 中的 GraphicBinaries 字段需要通过添加 { get; 成为 属性放; }.
这是一篇指出区别的文章:
What is the difference between a field and a property?