I am trying to pass a data structure from javascript back to C# using JQuery AJAX Post. The structure the javascript side looks like this:
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
});
The Equipment array is an array of 12 objects that look like this:
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;
Using the Chrome network tool, I see that the JSON data looks like this:
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
some array rows removed for clarity.
My Controller line looks like this:
public void _EquipmentScheduleLogDowntime(ReportedDowntime reportedDowntime)
My reported downtime structure looks like this:
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; }
Note the IEnumerable and the Array. I tried the array when the IEnumerable didn't work.
The AffectedEquipment looks like this:
public class AffectedEquipment
{
public int EquipmentId { get; set; }
public bool PrimaryCause { get; set; }
public bool Affected { get; set; }
public string EquipmentName { get; set; }
}
When the controller line is called, I get values for each of the top level elements in reported downtime. I get an array of AffectedEquipment of the proper length, but the values of each of the elements is 0 or null, as appropriate. Can anyone tell me why the child elements are not populating?
I found an answer at this question: Passing array to controller using jquery Post
I changed my post syntax to include contentType:'application/json', data:JSON.stringify(dt) to pass JSON instead of the native format.
The total clause now looks like this:
$.ajax({
type: 'POST',
contentType:'application/json',
data:JSON.stringify(dt),
url: url,
async: true,
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.