ASP.NET MVC - comma separated double as parameter of controller action

1.2k Views Asked by At

I have the following controller action:

public ActionResult AjaxQuantity(int productId = 0, double quantity = 0d, int periodId = 0)
{
   ...
}

and appropriate ajax request:

function Quantity(productId, quantity, periodId) {
    $.ajax({
        url: "@(Url.Action("AjaxQuantity"))",
        cache: false,
        type: "GET",
        data: { productId: productId, quantity: quantity, periodId: periodId },
        error: function () {
            Server503();
        }
    });
};

Also, have a culture with a comma as decimal separator.

When I do ajax request with, for example, "12,34" as quantity, inside controller action I get quantity as 1234d.

If I change type of ajax request to "POST", then I get desired quantity inside action as 12,34d.

What's happening with GET request? Look's like in GET request culture not used (comma is simple stript off).

1

There are 1 best solutions below

1
Dmitry Zaets On

The thing is that ',' is a reserved URI character so you are not able to use it in GET parameters.

POST parameters are send as request body, because of that ',' could be used there as well.

From Uniform Resource Identifiers (URI): Generic Syntax 2.2. Reserved Characters

   Many URI include components consisting of or delimited by, certain
   special characters.  These characters are called "reserved", since
   their usage within the URI component is limited to their reserved
   purpose.  If the data for a URI component would conflict with the
   reserved purpose, then the conflicting data must be escaped before
   forming the URI.

      reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                    "$" | ","