1

I have a form where data is posted to a Web API endpoint. The form has two input fields: one for text, and one for a number.

On the Web API side, my object is set up as such:

public class Course
{
    public string Title { get; set }
    public double? Price { get; set; }
}

When I send the value "25,3" for the Price, I get the following error:

The value '25,3' is not valid for Price.

Sending a value of "25.3" work fine.

I am posting the form using jQuery form serialization.

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Rasmus Christensen
  • 8,321
  • 12
  • 51
  • 78

1 Answers1

0

You can specify the culture on the server in the web.config, as per this answer. You will need to set your culture to one that uses commas to indicate fractions (e.g. fr-CA).

If you want to be able to accept both . and , as separators, you will need to set Price as a string, and then parse the value yourself.

Community
  • 1
  • 1
Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
  • So I tried changing the culture for the json formatter and it works. But if I post $("someform").serialize as the data, it's not using the json serializer, but instead the serializer of the FormUrlEncodedFormatter I think. So this will still fail – Rasmus Christensen Nov 11 '14 at 22:55
  • The culture change is on the server side (in the web.config). As long as the value is hitting the server as "23,5", the parser on the web api should pick up the change. – Ryan Kohn Nov 11 '14 at 23:11
  • Currently I set the culture on the json formatter serializer. Did try setting it in config, but need to do further testing. It dosen't seem to do the same to culture settings.. – Rasmus Christensen Nov 13 '14 at 07:27