I get an error (“The field Amount must be a number”) on my web page on a currency field. It is because of the dollar sign ($50.00).
[DataType(DataType.Currency)]
[DisplayFormat(DataFormatString = "{0:c}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }
@Html.EditorFor(model => model.Amount)
What else do I need to do if I want to keep the dollar sign?
Default MVC model binder cannot parse value formatted for display. So, you should write your own model binder and register it for this type (suppose type name is Foo):
Add this binder for Foo type at Application_Start:
Ah, and last thing – remove
data-val-numberattribute from amount textbox (otherwise you will continue seeing message that it’s not a number):Now you will get validation error message if input value will not be correct currency amount (e.g.
$10F.0).BTW I think it’s better to use
ApplyFormatInEditMode = falsethan implement all this stuff to help MVC bind your custom formatted string.