有了這段代碼,我正試圖將Json數組發佈到我的web核心API中的一個方法。但數組到達爲空。無法發佈Json數組到web核心api
importProperties = function (url, callback, data) {
var properties = JSON.stringify(data);
$.ajax({
url: url,
type: "POST",
contentType: "application/json",
dataType: "json",
data: { "": properties }
})
.done(callback)
.fail(errorMessage);
}
一旦數據被JSON.stringified,它看起來像;
[{"UPRN":"12345","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B12345","PropertyNo":"1","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":1,"NumberOfBedrooms":2,"NumberOfKitchens":1,"PropertyContractId":0,"PropertyType":null},
{"UPRN":"67890","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B67890","PropertyNo":"2","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":null,"NumberOfBedrooms":null,"NumberOfKitchens":null,"PropertyContractId":0,"PropertyType":null}]
我的web核心API上的方法簽名是;
[HttpPost("{contractId}/import")]
public async Task<IActionResult> Import(int contractId, [FromBody] IEnumerable<PropertyAndContractDto> properties)
{
this.NLogger.Info($"api/property/contractId = {contractId}/saveproperties".ToPrefix());
if (properties == null)
{
return BadRequest();
}
...
}
的DTO是
public class PropertyAndContractDto
{
public string UPRN { get; set; }
public string StreetName { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string AddressLine4 { get; set; }
public string Postcode { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string BlockUPRN { get; set; }
public string PropertyNo { get; set; }
public string BlockName { get; set; }
public string Comments { get; set; }
public int? PropertyTypeId { get; set; }
public int? NumberOfBathrooms { get; set; }
public int? NumberOfBedrooms { get; set; }
public int? NumberOfKitchens { get; set; }
public int PropertyContractId { get; set; }
public string PropertyType { get; set; }
}
}
當我在郵差它的工作原理運行;
那麼,爲什麼性能參數爲空來嗎?
最有可能的,因爲它不能被解析成'PropertyAndContractDto'。發佈DTO課程。類屬性是否與字符串中的屬性相同? *可以*你將這個字符串解析成一個DTO數組來開始嗎? –
Mhh我猜模型綁定不起作用。你需要一個類型爲IEnumerable'的類。但不是100%確定。 –
我已經發布了Dto。 Web核心API和數據都是相同的類。 – arame3333