I'm trying pass a currency value to a decimal property, but the MVC removes the ",". I tried with ".", but the MVC removes too.
public ActionResult MyAction(decimal value)
{
}
I'm sending the ajax request to the following URL:
/MyAction?value=1000,35
But when I send the request, I received the 100035 value. Why?
When we pass the data which are of type string/ int/ decimal (with points like “1234.56”) to the back end, the MVC framework will map them to the corresponding model properties. When we come across use case, where we want to post the currency values like salary “1,234.56” MVC framework will fail to bind them to the corresponding model object.
By default, the following fields support pricing decimal precision: This option allows you to enable and set decimal precision to individual currencies. To define this setting, go to Settings > Business Management > Currencies. Open the currency record and define the precision for currency.
When a method is invoked using reflection, it has to try to match up the parameters you passed in with the method’s parameters. Because you have to pass doubles, and the method has decimal parameter, it has to convert these doubles into decimals. Apparently when you are invoking a method using reflection, it can’t handle this conversion.
More information: Manage transactions with multiple currencies This setting supports values between 0 and 4. This precision setting is applied to any currency field whose precision is set to Currency Precision in the field properties. To see the field properties, go to Settings > Customizations > Entity > Fields.
To resolve my problem, I used this article:
http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx/
I created one DecimalModelBinder like the article shows
Another thing that I did is converting the decimal to string before to send to action:
var url = string.Format("/MyAction?value={0}", decimalValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With