Having trouble sending decimal or double values via ajax to my C# MVC Controller. The values always come through as null, even though they work fine when sent as strings or integers. Why is this happening? When checking the client's request, the correct value is being sent (Form Data: price=84.50
).
Error:
The parameters dictionary contains a null entry for parameter 'price' of non-nullable type 'System.Decimal'
Html:
<input type="number" step="1" class="form-control" name="price" id="price">
<button type="button" class="btn btn-success">Send</button>
Javascript:
$('.btn-success').click(function () {
//var price = $('#price').val(); - Did not work
//var price = Number($('#price').val()); Did not work
var price = Number($('#price').val()).toFixed(2); // Does not work
$.ajax({
url: 'PriceFunction',
type: 'POST',
data: {
price: price,
}
}).done(function () {
}).fail(function () {
console.log("Error in ajaxfunction!");
});
});
C#:
[HttpPost]
public void PriceFunction(decimal price)
{
// I have tried with decimal, double and double?.
}