我有一个对象,我希望发送到一个WCF服务,但对象没有得到正确的转换,以在另一边使用的ActiveFarmer和Evidence总是空的。我用硬编码的JSON对它进行了测试,它可以工作,但击倒对象没有正确通过,对象如下
self.afi = {
ActiveFarmer: {
ActiveFarmerID: ko.observable(),
BusinessID: ko.observable(),
Year: ko.observable(),
StatusID: ko.observable(),
Status:ko.observable(),
DecisionDate: ko.observable(),
UserID: ko.observable(""),
DateModified: ko.observable(),
// RowStatus: ko.observable(""),
},
// TODO - RH we used to add a default empty new line here...
ActiveFarmerEvidence: ko.observableArray(),
NotepadIdentifiers: ko.observableArray()
};
我正在调用wcf服务
public int UpdateFarmerInfo(ActiveFarmerInfo afi)
{
return ActiveFarmerInfoManager.UpdateFarmerInfo(afi);
}
我的ajax调用如下
var afi = vm.afi;
$.ajax({
type: "POST",
contentType: "application/json",
url: rootcap + "ActiveFarmerService.svc/rest/UpdateFarmer",
data: JSON.stringify(afi),
dataType: "json",
async: true,
cache: false,
success: function (data, textStatus) {
self.populateFarmerDetails(data);
},
error: function (data, status, error) { }
});
整个视图模型
function ActiveFarmerModel() {
var self = this;
self.businessId = ko.observable();
self.individualId = ko.observable();
self.currentAppId = ko.observable();
self.userId = ko.observable();
self.evidenceCategories = ko.observableArray();
self.activeFarmerEvidenceCategories = ko.observableArray();
self.businessStatus = ko.observableArray([{ "intStatusID": "2", "vcrStatus": "Provisionally Non-Oct" }, { "intStatusID": "1", "vcrStatus": "Active" }, { "intStatusID": "3", "vcrStatus": "Negative List" }, { "intStatusID": "4", "vcrStatus": "Active (Non Verified)" }])
self.currentStatus = ko.observable();
self.oldStatus = self.currentStatus();
self.ActiveFarmerID = ko.observable();
self.currentYear = ko.observable();
self.years = ko.observableArray([{ "intYearID": "5", "year": "2014" }, { "intYearID": "6", "year": "2015" }]);
self.businessStartDate = ko.observable();
self.DateFromServer = ko.observable(moment(new Date()).format('L')),
self.individualDOB = ko.observable();
self.businessName = ko.observable();
self.countyOfficeId = ko.observable();
self.individualDetails = ko.observable();
self.indDobOuterHTML = ko.observable();
self.evidenceTypes = ko.observableArray();
self.evidenceItem = function (evidenceData) {
var self = this;
if (evidenceData === null) {
self.EvidenceCategoryID = ko.observable(-1);
self.DateModified = ko.observable(moment(new Date()).format('L'));
self.DateReceipted = ko.observable(moment(new Date()).format('L'));
self.DocReceiptID = ko.observable("");
self.EvidenceID = ko.observable(-1);
self.EvidenceTypeID = ko.observable(-1);
self.Other = ko.observable("");
self.ReceiptedBy = ko.observable("");
//self.RowStatus = ko.observable("I");
self.TrimRecord = ko.observable("");
self.TrimRecordDocReceipt = ko.observable("");
self.UserID = ko.observable('NIGOV1284535');
self.selectedCategory = ko.observable(6);
}
else {
self.EvidenceCategoryID = ko.observable(6);
self.DateModified = ko.observable(moment(evidenceData.DateModified).format('L'));
self.DateReceipted = ko.observable(moment(evidenceData.DateReceipted).format('L'));
self.DocReceiptID = ko.observable(evidenceData.DocReceiptID);
self.EvidenceID = ko.observable(evidenceData.EvidenceID);
self.EvidenceTypeID = ko.observable(evidenceData.EvidenceTypeID);
self.Other = ko.observable(evidenceData.Other);
self.ReceiptedBy = ko.observable(evidenceData.ReceiptedBy);
self.RowStatus = ko.observable(evidenceData.RowStatus);
self.TrimRecord = ko.observable(evidenceData.TrimRecord);
self.TrimRecordDocReceipt = ko.observable(evidenceData.TrimRecordDocReceipt);
self.UserID = ko.observable(evidenceData.UserID);
self.selectedCategory = ko.observable(6)
}
self.filteredSubCategories = ko.computed(function () {
self.EvidenceCategoryID = self.selectedCategory();
var ret = ko.observableArray();
var filterAll = ko.observableArray();
ret().push({ EvidenceTypeID: -1, EvidenceTypeDescription: "Please Select..." });
if (self.selectedCategory()) {
filterAll = ko.utils.arrayFilter(vm.evidenceTypes(), function (item) {
return item.EvidenceCategoryID === self.selectedCategory();
});
for (var i = 0; i < filterAll.length; i++) {
ret().push(
{
EvidenceTypeID: filterAll[i].EvidenceTypeID,
EvidenceTypeDescription: filterAll[i].EvidenceTypeDescription,
EvidenceCategoryID: filterAll[i].EvidenceCategoryID
});
};
return ret();
}
return [];
}, self);
};
self.NotePad = {
notes: ko.observableArray([
{ DateOfEntry: "01/05/2014", UserName: "John", EntryType: "Change Status", NoteText: "Status Changes to Active(Non Verified)" }
])
};
// Active Farmer Information
self.afi = {
ActiveFarmer: {
ActiveFarmerID: ko.observable(),
BusinessID: ko.observable(),
Year: ko.observable(),
StatusID: ko.observable(),
Status:ko.observable(),
DecisionDate: ko.observable(),
UserID: ko.observable(),
DateModified: ko.observable(),
},
ActiveFarmerEvidence: ko.observableArray(),
NotepadIdentifiers: ko.observableArray()
};
}
public class ActiveFarmerInfo
{
public ActiveFarmerInfo()
{
}
protected override string GetSchemaName()
{
return "ActiveFarmer";
}
[XmlIgnore]
public int ActiveFarmerID { get; set; }
[DataMember, XmlElement]
public ActiveFarmer ActiveFarmer { get; set; }
// To contain the list for Active Farmer Application Evidences based on Active Farmer Id and Year
[DataMember, XmlElement]
public List<Evidence> ActiveFarmerEvidence { get; set; }
public Int32 BusinessId { get; set; }
public Int32 Year { get; set; }
}
视图模型的所有属性都是knockout可观察函数。所以调用JSON.stringify
到一个视图模型用那种属性不会产生有效的json。尝试将JSON.strngify
更改为ko.toJSON
这个函数从knockout视图模型产生有效的json字符串。像这样:
var afi = vm.afi;
$.ajax({
type: "POST",
contentType: "application/json",
url: rootcap + "ActiveFarmerService.svc/rest/UpdateFarmer",
data: ko.toJSON(afi),
dataType: "json",
async: true,
cache: false,
success: function (data, textStatus) {
self.populateFarmerDetails(data);
},
error: function (data, status, error) { }
});
注意传递给$.ajax
函数的对象的data属性