views:

37

answers:

1

In my HTTP POST controller method for add/editing data, I'm receiving an entity from my EF data model. Some of the fields in that entity are of type double. Everything works fine unless the user types in a comma for a thousands separator, which then causes a model binding validation error.

Has anyone run into this before? Is replacing the default model binder with a custom one the only solution or is there a better approach?

Any advice would be greatly appreciated.

Thanks.

A: 

Modelbinding for MVC uses a culture invariant mode of binding. Therefore numbers must be in the 100000.00 format.

Though the following relates to the datetime it also is pertinent to the decimal type as well.

Quote from the asp.net team member:

"This is intentional. Anything that is part of the URI (note the 'Uniform' in URI) is interpreted as if it were coming from the invariant culture. This is so that a user in the U.S. who copies a link and sends it over IM to a friend in the U.K. can be confident that his friend will see the exact same page (as opposed to an HTTP 500 due to a DateTime conversion error, for example). In general, dates passed in RouteData or QueryString should be in the format yyyy-mm-dd so as to be unambiguous across cultures.

If you need to interpret a QueryString or RouteData parameter in a culture-aware manner, pull it in as a string, then convert it to the desired type manually, passing in the desired culture. (DateTime.Parse has overloads that allow you to specify a culture.) If you do this, I recommend also taking the desired culture as a QueryString or a RouteData parameter so that the 'Uniform' part of URI isn't lost, e.g. the URL will look something like ...?culture=fr-fr&date=01-10-1990."

BuildStarted