I am working with .Net 6 and Mvc. I have a problem with decimals, when bound from the view to the controller, it considers them integers. For example, from the view I send 25.6 to the controller it arrives 256.0
This my code.
<button type="button" id="btnConfirm">Confirm</button>
<input id="txtName" name="txtName" disabled="disabled">
<input id="txtAmount" name="txtAmount" disabled="disabled">
$('#btnConfirm').on('click', confirmTransaction['confirm']);
var confirmTransaction = {
'confirm': function (e) {
var data = {
Name: $('#txtName').val(),
Amount: $('#txtAmount').val()
}
SendTransaction(data);
}
}
function SendTransaction(transaction) {
return $.ajax({
url: '@Url.Action("InsertTransaction", "TransactionController")',
method: 'POST',
beforeSend: ShowLoading('Please wait.'),
complete: function (d) {
HideLoading();
},
success: function (data) {
console.log(data);
if (data.resultado !== 'OK') {
alertify.alert("Error!");
return;
}
toastr.success('Success.');
return;
},
error: function (data) {
alertify.alert('Error.');
HideLoading();
},
data: { transaction }
});
}
/*At this point, the decimals arrive as integers.*/
public JsonResult InsertTransaction(Transaction transaction)
{
/*More code*/
}
public class Transaction {
public string Name {get; set;}
[DisplayFormat(AppyFormatInEditMode=true, DataFormatString = "{0:C}")]
public decimal Amount {get; set;}
}
Thanks.