我一直试图将对象数组传递给在Controller类上创建的JsonResult操作方法,我尝试了来自不同答案的所有可能解决方案,但无济于事。 这是我的js函数:
function CalculateCost() {
var distancecost = [];
//prepare the List<DistanceCost> object
for (i = 0; i < _SELECTED_POINTS.length; i++) {
console.log('point::' + _SELECTED_POINTS[i]);
let dist = {
PlaceId: _SELECTED_POINTS[i].place_id,
PointSequence: _SELECTED_POINTS[i].PointSequence,
controlId: _SELECTED_POINTS[i].controlId,
FromLatitude: (i == (_SELECTED_POINTS.length - 1)) ? _SELECTED_POINTS[0].geometry.location.lat() : _SELECTED_POINTS[i].geometry.location.lat(),
FromLongitude: (i == (_SELECTED_POINTS.length - 1)) ? _SELECTED_POINTS[0].geometry.location.lng() : _SELECTED_POINTS[i].geometry.location.lng(),
ToLatitude: _SELECTED_POINTS[i].geometry.location.lat(),
ToLongitude: _SELECTED_POINTS[i].geometry.location.lng(),
DistanceType: 'Mile',
DistanceCalculateType: (i == (_SELECTED_POINTS.length - 1)) ? 'TotalDistance' : 'PickDrop',
TotalPrice: '0',
TotalDistance: '0'
};
console.log(dist);
distancecost.push({
dist
});
}
$.ajax({
method: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '/Dashboard/CalculateDistanceAndPrice',
data: JSON.parse(JSON.stringify({ 'distanceCost': distancecost })),
success: function (response) {
console.log('DistanceCalculation Response:: ' + JSON.stringify(response));
},
error: function (response) {
console.log(response);
}
});
}
这是Jsonresult操作方法:
编辑: 正如尼古拉斯建议的那样,我更改了类型,但仍然无法正常工作,在插入任何数据时使用了AFAIK POST,在更新任何数据时使用了PUT,但是这里我只是通过计算每个点之间的距离来获取数据:
$.ajax({
method: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: '/Dashboard/CalculateDistanceAndPrice',
data: JSON.stringify({ 'distanceCost': distancecost }),
success: function (response) {
console.log('DistanceCalculation Response:: ' + JSON.stringify(response));
},
error: function (response) {
console.log(response);
}
});
编辑: 我更改了对象的创建,但仍然没有运气:
var DistanceCost = new Object();
DistanceCost.PlaceId = _SELECTED_POINTS[i].place_id;
DistanceCost.PointSequence = _SELECTED_POINTS[i].PointSequence;
DistanceCost.controlId = "";//_SELECTED_POINTS[i].controlId,
DistanceCost.FromLatitude = (i == (_SELECTED_POINTS.length - 1)) ? _SELECTED_POINTS[0].geometry.location.lat() : _SELECTED_POINTS[i].geometry.location.lat();
DistanceCost.FromLongitude = (i == (_SELECTED_POINTS.length - 1)) ? _SELECTED_POINTS[0].geometry.location.lng() : _SELECTED_POINTS[i].geometry.location.lng();
DistanceCost.ToLatitude = _SELECTED_POINTS[i].geometry.location.lat();
DistanceCost.ToLongitude = _SELECTED_POINTS[i].geometry.location.lng();
DistanceCost.DistanceType = 'Mile';
DistanceCost.DistanceCalculateType = (i == (_SELECTED_POINTS.length - 1)) ? 'TotalDistance' : 'PickDrop';
DistanceCost.TotalPrice = '0';
DistanceCost.TotalDistance = '0';
答案 0 :(得分:0)
尝试删除distanceCost的报价
+> data: JSON.parse(JSON.stringify({ distanceCost: distancecost }))