【问题标题】:Ajax POST not parsing properlyAjax POST 无法正确解析
【发布时间】:2014-01-22 23:06:41
【问题描述】:

我正在尝试使用 JQuery AJAX Post 将数据结构从 javascript 传递回 C#。 javascript端的结构是这样的:

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;

使用 Chrome 网络工具,我看到 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

为了清楚起见,删除了一些数组行。

我的 Controller 行如下所示:

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,视情况而定。谁能告诉我为什么没有填充子元素?

【问题讨论】:

    标签: c# asp.net-mvc jquery


    【解决方案1】:

    我在这个问题上找到了答案:Passing array to controller using jquery Post

    我更改了我的帖子语法以包含 内容类型:'应用程序/json', 数据:JSON.stringify(dt) 传递 JSON 而不是原生格式。

    total 子句现在看起来像这样:

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

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      相关资源
      最近更新 更多