I have a problem that I checked some time ago and also posted on Microsoft Github since it seemed a bug, I'm speaking of years ago. I was checking if was solved but it seems no.
It is a trivial thing: I'm using Google Maps APIs with an autocomplete to retrieve an address, collecting the information along with latitude and longitude and pass it via jQuery POST to an MVC Controller (.net 6) to store it. All the values are deserialized fine, the latitude and longitude are interpreted in a wrong way, they lose the floating point. This happens when I use a combination of cultures which is different from en-US in the browser but it can happen also in other circumstances.
The code I use to send it, is briefly this:
var propertyToSave = { IDUser: 0 };
// GOOGLE AUTOCOMPLETE SELECTED PLACE IN VAR selectedPlace
console.log(selectedPlace.geometry.location.lat());
console.log(selectedPlace.geometry.location.lng());
propertyToSave.Latitude = selectedPlace.geometry.location.lat();
propertyToSave.Longitude = selectedPlace.geometry.location.lng();
// other properties...
$.ajax({
url: 'url',
data: { property: propertyToSave, __RequestVerificationToken: $('input[name=__RequestVerificationToken]').first().val() },
timeout: DEFAULT_AJAX_TIMEOUT,
dataType: 'json',
method: 'POST'
}).done(function (data, textStatus, jqXHR) {
// MANAGING RESPONSE
}).fail(function (jqXHR, textStatus, errorThrown) {
// MANAGING RESPONSE
});
At this point when I make the Ajax call everything streams fine until the controller deserializes the value, this is what I see in the browser console. Everything matches until the controller is deserializing the value and losing the floating point.
The json sent (converting latitude and longitude in the client as string with JSON.stringify to avoid any culture interference) looks like:
{
"IDProperty": 0,
"IDUser": 0,
...
"Latitude": "-34.8272987",
"Longitude": "-56.1201179",
...
}
The signature of the controller accepts an object that reflect exactly the json sent
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult> ExecuteSubmit(PropertyViewModel property)
Console Log:
Console XHR Call:
What the controller deserializes:
I mean it's weird that after many time the problems is not solved yet, I also tried to pass the value untouched as a string serializing it before sending it in this way but it didn't help
JSON.stringify(selectedPlace.geometry.location.lng())
Abyway faced this problem and solved it? The only way to solve I have found is to create other two string properties and sending it as string then deserializing it manually but it's a workaround
Many thanks