我在上一个问题的上面的链接中提出了问题,并且评论中的更改已得到解决。
Properties of class parameter are always null in Asp.Net Web Api
但这次问题有点不同。方法参数是另一个具有多个类类型属性的类。
[HttpPost]
[EnableCors(origins: "*", headers: "*", methods: "*", exposedHeaders: "X-Custom-Header")]
public IReportOutput InsuranceHandlingFiles([FromBody]InsuranceHandlingFilesInput insuranceHandlingFilesInput)
{
}
public class InsuranceHandlingFilesInput
{
public CaseCountsInput CaseCountsInput { get; set; }
public MioEurosInput MioEurosInput { get; set; }
public SharesInput SharesInput { get; set; }
}
以下列方式创建属于此类的Json对象后,我发送带有Post请求的帖子。但即使第一个属性正确填充,其他属性也为null。我该如何解决这个问题?
function GetReportData() {
var reportModel = {
CaseCountsInput: {
MainTitle: "Dosya Sayısı",
YearTitle: "Yıl",
InsurerFileCountTitle: "Sigortacı Dosya Sayısı",
TotalTitle: "Toplam",
GrandTotalTitle: "Genel Toplam",
InsurerTotalCount: 1,
GrandTotal: 3,
FileCounts: []
},
MioEurosInput :{
MainTitle:"Incurred Sums - Mio Euro",
YearTitle:"Year",
InsurerIncurredTitle:"Insurer Incurred",
TotalTitle:"Total",
GrandTotalTitle:"Grand Total",
MioEuros :[],
InsurerIncurredTotal:0.11,
GrandTotal:0.28
},
SharesInput :{
MainTitle:"Incurred Shares (%)",
YearTitle:"Year",
InsurerIncurredShareTitle:"Insurer Incurred (%)",
TotalTitle:"Total",
GrandTotalTitle:"Grand Total",
ShareDatas:[],
InsurerIncurredShareTotal:39.29,
GrandTotal:100
}
}
var caseCounts =[];
caseCounts.push({
"year": 2014,
"InsurerFilesCount": 1
});
caseCounts.push({
"year": 2015,
"InsurerFilesCount": 4
});
for (var i = 0; i < caseCounts.length; i++) {
reportModel.CaseCountsInput.FileCounts.push(caseCounts[i]);
}
var mioEuroData =[];
mioEuroData.push({
"year": 2016,
"InsurerMioEuro": 0
});
mioEuroData.push({
"year": 2017,
"InsurerMioEuro": 0.11
});
for (var i = 0; i < mioEuroData.length; i++) {
reportModel.MioEurosInput.MioEuros.push(mioEuroData[i]);
}
var shares=[];
shares.push({
"year": 2016,
"insurerShare": 0
});
shares.push({
"year": 2017,
"insurerShare": 45.83
});
for (var i = 0; i < shares.length; i++) {
reportModel.SharesInput.ShareDatas.push({year:shares[i].year,InsurerIncurredShare:shares[i].insurerShare,ArcelikIncurredShare:shares[i].arcelikShare});
}
return reportModel;
}
$("#btnExport")
.click(function() {
var reportData = GetReportData();
console.log(JSON.stringify(reportData));
$.ajax({
type: "POST",
dataType: "json",
data: JSON.stringify({CaseCountsInput:reportData.CaseCountsInput,MioEurosInput:reportData.MioEurosInput,SharesInput:reportData.SharesInput}),
contentType: "application/json",
url: "http://localhost:50773/api/export/insurancehandlingfiles",
success: function(data) {
var DocumentBody = data.ReportData;
var FileName = data.FileName;
dataURItoBlob(DocumentBody, FileName);
},
error: function(error,as,asd) {
jsonValue = jQuery.parseJSON(error.responseText);
alert("error" + error.responseText);
}
});
});