I have a class defined:
public class custom_field
{
public long custom_field_type_id { get; set; }
}
In my controller I have the following Ajax called method:
[HttpPost]
public JsonResult CustomFieldEdit(long hash)
{
var respObj = new custom_field();
respObj.custom_field_type_id = -8454757700450211158L;
return Json(respObj);
}
My jQuery that calls CustomFieldEdit
var baseUrl = '@Url.Action("CustomFieldEdit")?hash=' + customFieldId;
$.ajax({
type: "POST",
url: baseUrl,
contentType: "application/json",
data: JSON.stringify({ hash: customFieldId }),
error: function (xhr, status, error) {
toastr.error("Error saving, please try again.");
},
success: function (data) {
console.log(data.custom_field_type_id); //error here! val = -8454757700450211000
}
});
So the long value in the controller is -8454757700450211158
but the value parsed to JSON is -8454757700450211000
.
I know I can fix this by changing custom_field_type_id to a string or creating a JSON DTO with a string property for long properties but I would like to know another way for fixing this if possible, like a Newtonsoft JSON serializer setting.