通过序列化我的一个JavaScript对象创建的POST字符串在发布到我的网页时看起来像这样:
bikeID=1&vehicleID=23&VIN=1HD1FB416AB666666&slotNum=0&alias=Betsy&BikeCustomizations[0][ CustomizationTypeID] =1&BikeCustomizations[0][ ManufacturerID] =4&BikeCustomizations[0][ CustomizationSubTypeID] =1&BikeCustomizations[1][ CustomizationTypeID] =2&BikeCustomizations[1][ ManufacturerID] =-1
这是发布到我的ASP.NET MVC 3应用程序。我得到了基本属性“bikeID,我在BikeCustomizations
中获得了正确的项目数(在这种情况下为2),但这些对象没有值。
如果我更改POST字符串并重新发布(使用Fiddler)到下面,反序列化工作正常:
bikeID=1&vehicleID=23&VIN=1HD1FB416AB666666&slotNum=0&alias=Betsy&BikeCustomizations[0].CustomizationTypeID=1&BikeCustomizations[0].ManufacturerID=4&BikeCustomizations[0].CustomizationSubTypeID=1&BikeCustomizations[1].CustomizationTypeID=2&BikeCustomizations[1].ManufacturerID=-1
您可以看到差异为BikeCustomizations[1][ CustomizationTypeID] =2
与BikeCustomizations[1].CustomizationTypeID=2
。
问题:有没有办法强制对象的字符串化不使用funky数组样式属性setter语法?
以下是对象创建的JavaScript:
Cust.buildBike = function (bikeObj) {
if (bikeObj != undefined)
this.bike = bikeObj;
else
this.bike = { bikeID: -1 };
this.bike.BikeCustomizations = new Array();
var that = this;
$.each(this.customizationsArr, function (key, obj) {
if (obj == undefined)
return true;
var typeID = obj.ID,
SubTypeID = obj.SubTypeID,
ManufacturerID = obj.ManufacturerID,
Model = obj.Model;
if (ManufacturerID == -1)
SubTypeID = undefined;
that.bike.BikeCustomizations.push({
CustomizationTypeID: typeID,
ManufacturerID: ManufacturerID,
CustomizationSubTypeID: SubTypeID,
Model: Model
});
});
return this.bike;
}
...
$('#customizationPageNext').click(function () {
var data = Cust.buildBike(bikeObj);
$.mobile.changePage('/Selection/Calibrations/', { data: data });
});