0

I'm not able to pass a double to my action (at least not in a proper way).

This is my action:

  public JsonResult SomeAction(double? n = null)
  {
      // ...
  }

And this is my ajax call:

$.ajax({
    async: true,
    type: 'POST',
    url: '/SomeController/SomeAction',
    data: {
        "n": 1.5
    },
    success: function (data) {
        ...
    }
});

The number 1.5 is not being passed to the action. When debugging in serverside, I'm receiving a null instead.

If I change the ajax to this:

$.ajax({
    async: true,
    type: 'POST',
    url: '/SomeController/SomeAction',
    data: {
        "n": "1,5"
    },
    success: function (data) {
        ...
    }
});

I do get a 1.5 in serverside.

What change in serverside do I need to do so that the first ajax call works?

Another thing that worked for me was using the first ajax call and changing the signature of the action to this:

public JsonResult SomeAction(string n = "")
{
       double parsedN = Convert.ToDouble(n);
}

Of course both solutions suck

sports
  • 7,851
  • 14
  • 72
  • 129

1 Answers1

2

What finally worked was the answer of this other question:
ASP.NET MVC Form and double fields

Quoting here:

Might be caused by cultureinfo, some culture use , instead of . for decimal separator. try setting the following in web.config,

  <system.web>
    <globalization culture="en-US" uiCulture="en-US" />
  </system.web>
Community
  • 1
  • 1
sports
  • 7,851
  • 14
  • 72
  • 129