2

我正在尝试在我的操作中解析一些 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; }
}   

我正在从我的淘汰赛/打字稿脚本中发送 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参数只是保持为空。我发现如果我注释掉SubtitleAssignmentsand TranslationAssignmentsin SubmitQuotationModel,它反序列化 json 的其他部分就好了。但是我不明白为什么它不会在...Assignments没有注释掉的两个声明的情况下反序列化。

4

1 回答 1

2

SubtitleAssignments 和 TranslationAssignments 不是 json 中的列表,而是模型中的列表。他们只需要存在AssignmentModel而不是List<AssignmentModel>

于 2017-09-19T15:00:01.807 回答