C# MVC Controller cannot get decimal or double values from Ajax POST request
You need to stringify you data when you are sending decimal values.
data: JSON.stringify({ Price: 5.0 })
This is because the decimal is considered an integer by the default binder.
You could of course change to using the DecimalModelBinder
which is detailed at the following link:
ASP.NET MVC3 JSON decimal binding woes
This could be a Culture issue
Be sure that the string you are sending to your action is compliant to the current Culture. (check the decimal number separators .
,
)
Exemple
e.g. on a french server, 99.1
will not be understood as 99,1
, but will be converted to 0
.
Solution
In that case, one solution is to define culture in your Web.Config
<system.web>
...
<globalization uiCulture="en" culture="en-US"/>
</system.web>
Or, replacing the separator by the proper one on the client side.
I suggest trying to pass the data as JSON.
data: JSON.stringify({ price: price }),
contentType: "application/json; charset=utf-8"
Just pay attention to include the content type. It may be required in order to the binder know how to parse the data of your request.