将JSON对象传递给c#方法(使用集合作为参数(使用JQueryWidgets))时出现的奇怪问题
本文关键字:JQueryWidgets 使用 参数 问题 对象 JSON 集合 方法 | 更新日期: 2023-09-27 18:19:31
我在传递以下内容时遇到一个奇怪的问题:
queueNotificationData = {
StartDate: that.batchData.StartDate.valueOf(),
StartTime: that.batchData.StartTime.valueOf(),
EndDate: that.batchData.EndDate.valueOf(),
EndTime: that.batchData.EndTime.valueOf(),
ETR: that.batchData.ETR.valueOf(),
PTW: that.batchData.PTW.valueOf(),
SelectedTemplate: that.batchData.SelectedTemplate.valueOf(),
IncidentFlag: that.batchData.IncidentFlag.valueOf(),
IncidentNumber: that.batchData.IncidentNumber.valueOf(),
SendToSubscriber: that.batchData.SendToSubscriber.valueOf(),
SendToCustomer: that.batchData.SendToCustomer.valueOf(),
SendToSMC: that.batchData.SendToSMC.valueOf(),
BatchServiceIds: that.serviceIds,
DescriptionOfWorks: that.batchData.DescriptionOfWorks.valueOf(),
AffectedCustomerVOs: that.customerVOs
}
问题出在AffectedCustomerVOs参数上——这是从以前的调用中检索到的,并通过一系列小部件传递(它是一个相当长的向导表单的一部分)
这是调用c#方法来实际进行处理的代码:
this.options.sendRequest({
url: this.options.dataUrl,
data: queueNotificationData,
cache: false,
dataType: 'json',
success: function (data) {
that.data = data;
that._showSavedMessage();
},
error: function () {
that._showErrorMessage();
}
});
这里是c#方法:
[HttpPost]
[Authorize]
[ValidateInput(false)]
public JsonResult QueueNotificationBatch(QueueNotificationInputModel param)
{
//do some work - code not included
}
其中QueueNotificationInputModel是
public class QueueNotificationInputModel
{
public string BatchServiceIds { get; set; }
public List<CustomerVO> AffectedCustomerVOs { get; set; }
public string StartDate { get; set; }
public string StartTime { get; set; }
public string EndDate { get; set; }
public string EndTime { get; set; }
public string ETR { get; set; }
public string PTW { get; set; }
public string SelectedTemplate { get; set; }
public string IncidentFlag { get; set; }
public string IncidentNumber { get; set; }
public bool SendToSubscriber { get; set; }
public bool SendToCustomer { get; set; }
public bool SendToSMC { get; set; }
public string DescriptionOfWorks { get; set; }
public QueueNotificationInputModel()
{
AffectedCustomerVOs = new List<CustomerVO>();
}
}
现在,所有这些代码似乎都能很好地工作——成功地调用了C#方法,并且传入的值除了AffectedCustomerVO之外都很好。列表中有3个项(这是正确的),但列表中的项没有值-都是null/0。如果我发出警报(即.customerVOs[0][电子邮件]);在创建queueNotificationData对象之前,它会正确显示"test@test.com"但这个值从未进入c方法。
我想这是某种序列化问题,但我不知道在哪里?我们将非常感谢
尝试将此复杂对象作为JSON序列化发送:
this.options.sendRequest({
url: this.options.dataUrl,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(queueNotificationData),
cache: false,
dataType: 'json',
success: function (data) {
that.data = data;
that._showSavedMessage();
},
error: function () {
that._showErrorMessage();
}
});
这里显示的JSON.stringify
方法是在现代浏览器中原生构建的。如果您需要支持传统浏览器,您可能需要在页面中包含json2.js脚本。