0

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

enter image description here

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • What does `data` look like after you have loaded it using `$.param()`? – Lex Apr 20 '18 at 17:03
  • @lex dont know if you can understand it here. But looks ok `avl_list%5B0%5D%5Btracker_avl_id%5D=2126&avl_list%5B0%5D%5Bcar_id%5D=712&avl_list%5B0%5D%5Bx_lat%5D=-66.616149&avl_list%5B0%5D%5By_long%5D=9.25226&avl_list%5B0%5D%5Bazimuth%5D=173&avl_list%5B0%5D%5Bevent_time%5D=2016-09-28+06%3A02&avl_list%5B1%5D%5Btracker_avl_id%5D=739125&avl_list%5B1%5D%5Bcar_id%5D=7203&avl_list%5B1%5D%5Bx_lat%5D=-68.003599&avl_list%5B1%5D%5By_long%5D=9.043924&avl_list%5B1%5D%5Bazimuth%5D=1&avl_list%5B1%5D%5Bevent_time%5D=2016-09-28+05%3A03` – Juan Carlos Oropeza Apr 20 '18 at 17:11
  • Yeah, bummer. I guess I was hoping it had munged the lat/long parameters. I'm stumped. Is this a Web API 2 backend? Just throwing this out there, but we have never had to serialize or add any configuration stuff (and we return an IHttpActionResult). – Lex Apr 20 '18 at 17:16
  • @lex as OJ suggest I try changing from decimal to string and get the data. But that doesnt feel right. Its a MVC 5 controller with additional actions – Juan Carlos Oropeza Apr 20 '18 at 17:32

1 Answers1

0

If the culture of the computer you're on is not English, but something like Spanish, then a value of -68.504806 is not a valid number.

There are several solutions to this: one simpler solution is to bind it to a string property in the model first, then just use code to map it to the correct number:

string x_lat_input = String.Empty; // use this in your model property

var x_lat = Convert.ToDecimal(x_lat_input, CultureInfo.CurrentCulture);
OJ Raqueño
  • 4,471
  • 2
  • 17
  • 30