Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Problems to pass currency in decimal parameter

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?

like image 778
Renato Leite Avatar asked Dec 27 '15 13:12

Renato Leite


People also ask

How does MVC framework handle currency data?

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.

How to enable pricing decimal precision for individual currencies?

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.

Why can’t I call a method with a decimal parameter?

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.

How do I manage transactions with multiple currencies?

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.


1 Answers

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);
like image 79
Renato Leite Avatar answered Nov 10 '22 06:11

Renato Leite