3

I created a ViewModel object that has a decimal field containing a price. When I post that to my controller, this is what happens:

  • Enter "15" -> ok! Controller receives 15.
  • Enter "15.00" -> not ok! Controller receives a 'null' field.
  • Enter "15,00" -> validation error because the field should be formatted with a period (I just stick to one formatting type to avoid complexity for the time being).
  • Enter "15.00M" -> validation error, probably because it's not considered to be a number.

How do I fix this? I want "15.00" to be a correct value, but I can't figure out how to do this. I tried a couple of custom modelbinders that I found on the interweb, but they didn't work.

5
  • Is there a reason you want to use a decimal instead of a double? Commented May 18, 2012 at 18:28
  • Well, it's a 'money' value. I learned always to use decimal for money values. Commented May 18, 2012 at 18:28
  • The value "15.00" is not a valid decimal but "15.00M" is a valid decimal. Try it, decimal test = 15.00; will show an error. The model binder you're using probably isn't converting it. Now, if you did decimal.parse("15.00") it should work. Commented May 18, 2012 at 18:32
  • Does not matter since conversion is done by the MVC engine. The only thing that matters is IF the MVC engine is able to construct the required model from the given values (from query string or name / value pairs). Commented May 18, 2012 at 18:33
  • Show your view model and your action method. Commented May 18, 2012 at 18:55

2 Answers 2

5

Easiest way to fix this is to pin the language in the web.config file:

<globalization culture="en-us" uiCulture="en-us" />

you have to place this one in the <system.web> node.

Why is it the easiest? This way the included JavaScript helpers can do validation without problems (which is done by assuming that numbers should be US formatted values), which is now (due to our changes) the same on the server. Therefore something that is valid on the client side will also be valid on the server side (statement only valid for simple cases and JavaScript enabled browsers).

All other options involve more editing, extending and knowledge about localization in MVC 3.

Sign up to request clarification or add additional context in comments.

2 Comments

Well, that was easy. Thank you!
One remark - there is a difference between uiCulture and culture (of course! :D). If you still need to support different languages you can set the uiCulture to auto, i.e. culture="en-us" uiCulture="auto". That way you can have custom UI strings (localization) but still have (US) English formatted numbers and such.
0

Create a field of type string not mapped, like the following

[NotMapped]
public string priceComputed {get;set}

then in your controller, you set value at right field

price = convert.todecimal(pricecomputed)

1 Comment

it's not a real solution , when you use string instead of float ,validations don't work correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.