Im migrating an application so this code was already working.
I have one angular function to create random avl input to test my api.
$scope.createAvl = function () {
console.log($scope.avl); // for debug
$scope.avl.push({
'tracker_avl_id': getRandomArbitrary(0, 1000000, 1),
'car_id': getRandomArbitrary(0, 10000, 1),
'x_lat': getRandomArbitrary(-69, -66, 0),
'y_long': getRandomArbitrary(8, 10, 0),
'azimuth': getRandomArbitrary(0, 359, 1),
'event_time': getRandomDate()
});
};
As you can see the data is created and show properly on the page
Then the Save Avl function to send the data
$scope.saveAvl = function () {
var data = $.param({
avl_list: $scope.avl
});
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
};
$http.post('/Avl_Api/traffic_tracker_avl/putTrafficAvl', data, config)
.success(function (data, status, headers, config) {
console.log('Sucess.....');
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
};
The problem is on my controller Im getting 0 for coordinates x, y for all elements on avl_list
all other fields are ok.
public partial class traffic_tracker_avl
{
public int avl_id { get; set; }
public long tracker_avl_id { get; set; }
public long car_id { get; set; }
public decimal x_lat { get; set; }
public decimal y_long { get; set; }
public int azimuth { get; set; }
public System.DateTime event_time { get; set; }
public Nullable<System.DateTime> created_at { get; set; }
}
[HttpPost]
public JsonResult putTrafficAvl(List<traffic_tracker_avl> avl_list)
{
avl_list.ForEach(n => db.traffic_tracker_avl.Add(n));
db.SaveChanges();
}
Im guessing have something to do with the decimal field? But is weird get 0 instead of truncate to integer. I double check and the data and the model field name are the same.