2017-08-17 46 views
3

有了這段代碼,我正試圖將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; } 
    } 
} 

當我在郵差它的工作原理運行;

enter image description here

那麼,爲什麼性能參數爲空來嗎?

+0

最有可能的,因爲它不能被解析成'PropertyAndContractDto'。發佈DTO課程。類屬性是否與字符串中的屬性相同? *可以*你將這個字符串解析成一個DTO數組來開始嗎? –

+0

Mhh我猜模型綁定不起作用。你需要一個類型爲IEnumerable '的類。但不是100%確定。 –

+0

我已經發布了Dto。 Web核心API和數據都是相同的類。 – arame3333

回答

2

嘗試Ajax調用的processData屬性設置爲false,像這樣:

$.ajax({ 
      url: url, 
      type: "POST", 
      contentType: "application/json", 
      dataType: "json", 
      data: { "": properties }, 
      processData: false 
     }) 

按照docs:在傳遞到數據選項作爲對象

默認情況下,數據 (技術上,除字符串以外的任何內容)將被處理,並且 轉換爲查詢字符串,適合默認內容類型 「application/x-www-form-urlencoded」。如果要發送一個 DOMDocument或其他未處理的數據,請將此選項設置爲false。

或者,只是data屬性設置爲你的字符串化JSON:

$.ajax({ 
      url: url, 
      type: "POST", 
      contentType: "application/json", 
      dataType: "json", 
      data: properties 
     }) 
+0

你的第二個建議就是答案。我以爲我嘗試了所有的選擇,但經過一些更正之後,我沒有恢復到這一個。非常感謝你的幫忙。 – arame3333