2014-01-22 106 views
1

我想通過使用jQuery AJAX Post將JavaScript數據結構傳遞迴C#。結構的JavaScript側看起來像這樣:Ajax POST不能正確解析

var dt = {}; 
dt.FailureMode = downtime.general; 
dt.FailureDetailId = downtime.detail; 
dt.Start = shared_formatDateWithTime(downtime.startTime); 
dt.End = shared_formatDateWithTime(downtime.endTime); 
dt.FailureDescription = downtime.description; 
dt.FailureSourceId = downtime.source; 
dt.FailureReporterId = downtime.reporter; 
dt.DowntimeId = downtime.downtimeId; 
dt.EquipmentArray = dtBuildUploadArray(downtime.associatedEquip); 
dt.ProcessUnitId = window.downtimeConfig.ProcessUnitId;; 

shared_showWorkingWindow(); 
var url = window.WEBROOT + 'Equipment/_EquipmentScheduleLogDowntime'; 
$.ajax({ 
    type: 'POST', 
    data:dt, 
    url: url, 
    async: true 
}); 

設備陣列12米的物體看起來像這樣的陣列:

var equip = new Object(); 
equip.EquipmentId = window.downtimeConfig.AssociatedEquipments[i].Id; 
equip.EquipmentName = window.downtimeConfig.AssociatedEquipments[i].Name; 
if (primary == i) { 
    equip.PrimaryCause = 1; 
} else { 
    equip.PrimaryCause = 0; 
} 
equip.Affected = 1; 
ar[ar.length] = equip; 

使用鉻網絡工具,我看到JSON數據長相像這樣:

FailureMode:1 
FailureDetailId:1 
Start:1/22/2014 5:50 PM 
End:1/22/2014 5:50 PM 
FailureDescription: 
FailureSourceId:1 
FailureReporterId:1 
DowntimeId:0 
EquipmentArray[0][EquipmentId]:1 
EquipmentArray[0][EquipmentName]:CR SGS 1 
EquipmentArray[0][PrimaryCause]:0 
EquipmentArray[0][Affected]:1 
EquipmentArray[1][EquipmentId]:2 
EquipmentArray[1][EquipmentName]:CR Alkon 1 
EquipmentArray[1][PrimaryCause]:0 
EquipmentArray[1][Affected]:1 
EquipmentArray[2][EquipmentId]:5 
EquipmentArray[2][EquipmentName]:CR Block Machine 1 
EquipmentArray[2][PrimaryCause]:0 
EquipmentArray[2][Affected]:1 
ProcessUnitId:1 

爲清楚起見刪除了一些數組行。

我控制器產品線看起來是這樣的:

public void _EquipmentScheduleLogDowntime(ReportedDowntime reportedDowntime) 

我的報道停機時間結構是這樣的:

public class ReportedDowntime : DatabagUtility 
{ 
    public int FailureMode { get; set; } 
    public int FailureDetailId { get; set; } 
    public DateTime Start { get; set; } 
    public DateTime? End { get; set; } 
    public string FailureDescription { get; set; } 
    public int FailureSourceId { get; set; } 
    public int FailureReporterId { get; set; } 
    public int DowntimeId { get; set; } 
    public string DowntimeColor { get; set; } 
    public string FailureSourceDescription { get; set; } 
    public string FailureReporterDescription { get; set; } 
    public string FailureModeDescription { get; set; } 
    public string FailureDetailDescription { get; set; } 
    public IEnumerable<AffectedEquipment> Equipments { get; set; } 
    public AffectedEquipment[] EquipmentArray { get; set; } 
    public int ProcessUnitId { get; set; } 

注意IEnumerable和陣列。 IEnumerable無法工作時,我嘗試了數組。

的AffectedEquipment看起來是這樣的:

public class AffectedEquipment 
{ 
    public int EquipmentId { get; set; } 
    public bool PrimaryCause { get; set; } 
    public bool Affected { get; set; } 
    public string EquipmentName { get; set; } 
} 

當控制器線被調用時,我得到的值在每一個報告停機頂級元素。我得到了適當長度的AffectedEquipment數組,但每個元素的值都是0或null。任何人都可以告訴我爲什麼子元素沒有填充?

回答

0

,我發現這個問題的答案:Passing array to controller using jquery Post

我改變了我的文章語法包括 的contentType: '應用/ JSON', 數據:JSON.stringify(DT) 通過JSON而不是原生的格式。

總子句現在看起來是這樣的:

$.ajax({ 
    type: 'POST', 
    contentType:'application/json', 
    data:JSON.stringify(dt), 
    url: url, 
    async: true, 
});