2014-01-22 68 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, 
});