正在解析 [FromBody] JSON: 模型为空
Parsing [FromBody] JSON: model null
我正在尝试在我的操作中解析一些 json,然后用它来做一些事情。但是,我一直将 null
作为我的模型,而不是填写的模型。
这是我要解析的 json:
{
"sameLanguages":true,
"sameDeadlines":true,
"sameDeliverables":false,
"quotations":[
{
"name":"zasd",
"deliverable":"538184e1-9a62-4ce9-baa7-ed746f267a9a",
"subtitleAssignments":{
"languageCombinations":[
{
"from":"d177b276-8f10-472f-84c6-f2ef59052a09",
"to":"d177b276-8f10-472f-84c6-f2ef59052a09",
"startDate":"19-09-2017",
"endDate":"19-09-2017"
}
],
"amount":12
},
"translationAssignments":{
"languageCombinations":[
]
}
}
]
}
这是我的操作:
[HttpPost]
public IActionResult Add([FromBody] SubmitQuotationsModel model)
{
//Do things...
return View();
}
这些是我的模型:
public class SubmitQuotationsModel
{
public bool SameLanguages { get; set; }
public bool SameDeadlines { get; set; }
public bool SameDeliverables { get; set; }
public List<SubmitQuotationModel> Quotations { get; set; } = new List<SubmitQuotationModel>();
}
public class SubmitQuotationModel
{
public string Name { get; set; }
public string Deliverable { get; set; }
public List<AssignmentModel> SubtitleAssignments { get; set; }
public List<AssignmentModel> TranslationAssignments { get; set; }
}
public class AssignmentModel
{
public List<LanguageCombinationModel> LanguageCombinations { get; set; }
public int Amount { get; set; }
}
public class LanguageCombinationModel
{
public string From { get; set; }
public string To { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}
我正在从我的 knockout/typescript 脚本中发送 json:
fetch('/Quotation/Add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: this.toJSON()
});
public toJSON(): string {
let model = {
sameLanguages: this.step1().sameLanguages(),
sameDeadlines: this.step1().sameDeadlines(),
sameDeliverables: this.step1().sameDeliverables(),
quotations: this.step2().quotations().filter((q) => q.isFilledIn()).map((q) => {
return {
name: q.name(),
deliverable: q.selectedDeliverable().id,
subtitleAssignments: this.getAssignmentModel(q.subtitleAssignmentGroup()),
translationAssignments: this.getAssignmentModel(q.translationAssignmentGroup())
}
})
};
return ko.toJSON(model);
}
private getAssignmentModel(model: AssignmentGroupModel) {
return {
languageCombinations: model.assignments().map((a) => {
return {
from: a.fromLanguage().value,
to: a.toLanguage().value,
startDate: a.startDate().format('DD-MM-YYYY'),
endDate: a.endDate().format('DD-MM-YYYY')
}
}),
amount: model.amount()
}
}
我没有收到任何异常,model
参数仍然为空。我发现如果我在 SubmitQuotationModel
中注释掉 SubtitleAssignments
和 TranslationAssignments
,它会很好地反序列化 json 的其他部分。但是我不明白为什么它不会反序列化那两个 ...Assignments
声明没有注释掉。
SubtitleAssignments 和 TranslationAssignments 不是 json 中的列表,但它们是模型中的列表。他们只需要 AssignmentModel
而不是 List<AssignmentModel>
我正在尝试在我的操作中解析一些 json,然后用它来做一些事情。但是,我一直将 null
作为我的模型,而不是填写的模型。
这是我要解析的 json:
{
"sameLanguages":true,
"sameDeadlines":true,
"sameDeliverables":false,
"quotations":[
{
"name":"zasd",
"deliverable":"538184e1-9a62-4ce9-baa7-ed746f267a9a",
"subtitleAssignments":{
"languageCombinations":[
{
"from":"d177b276-8f10-472f-84c6-f2ef59052a09",
"to":"d177b276-8f10-472f-84c6-f2ef59052a09",
"startDate":"19-09-2017",
"endDate":"19-09-2017"
}
],
"amount":12
},
"translationAssignments":{
"languageCombinations":[
]
}
}
]
}
这是我的操作:
[HttpPost]
public IActionResult Add([FromBody] SubmitQuotationsModel model)
{
//Do things...
return View();
}
这些是我的模型:
public class SubmitQuotationsModel
{
public bool SameLanguages { get; set; }
public bool SameDeadlines { get; set; }
public bool SameDeliverables { get; set; }
public List<SubmitQuotationModel> Quotations { get; set; } = new List<SubmitQuotationModel>();
}
public class SubmitQuotationModel
{
public string Name { get; set; }
public string Deliverable { get; set; }
public List<AssignmentModel> SubtitleAssignments { get; set; }
public List<AssignmentModel> TranslationAssignments { get; set; }
}
public class AssignmentModel
{
public List<LanguageCombinationModel> LanguageCombinations { get; set; }
public int Amount { get; set; }
}
public class LanguageCombinationModel
{
public string From { get; set; }
public string To { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}
我正在从我的 knockout/typescript 脚本中发送 json:
fetch('/Quotation/Add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: this.toJSON()
});
public toJSON(): string {
let model = {
sameLanguages: this.step1().sameLanguages(),
sameDeadlines: this.step1().sameDeadlines(),
sameDeliverables: this.step1().sameDeliverables(),
quotations: this.step2().quotations().filter((q) => q.isFilledIn()).map((q) => {
return {
name: q.name(),
deliverable: q.selectedDeliverable().id,
subtitleAssignments: this.getAssignmentModel(q.subtitleAssignmentGroup()),
translationAssignments: this.getAssignmentModel(q.translationAssignmentGroup())
}
})
};
return ko.toJSON(model);
}
private getAssignmentModel(model: AssignmentGroupModel) {
return {
languageCombinations: model.assignments().map((a) => {
return {
from: a.fromLanguage().value,
to: a.toLanguage().value,
startDate: a.startDate().format('DD-MM-YYYY'),
endDate: a.endDate().format('DD-MM-YYYY')
}
}),
amount: model.amount()
}
}
我没有收到任何异常,model
参数仍然为空。我发现如果我在 SubmitQuotationModel
中注释掉 SubtitleAssignments
和 TranslationAssignments
,它会很好地反序列化 json 的其他部分。但是我不明白为什么它不会反序列化那两个 ...Assignments
声明没有注释掉。
SubtitleAssignments 和 TranslationAssignments 不是 json 中的列表,但它们是模型中的列表。他们只需要 AssignmentModel
而不是 List<AssignmentModel>