Please tell me,
in my form, the user should enter percentage value between 1 to 100.
For that matter, if the user entered 80% than I want to save it in the database as 0.80 – I want a different value when saving to the database.
How can I do it?
My app is MVC 3 and EF Code-First based.
Thank you
It doesn’t exists “as is”.
What you could try (they are really many other ways to do that, by the way).
Create a Custom Attribute (PercentageAttribute) that you will put on
your “percentage” properties
Create a Custom ModelBinder, override the SetProperty, and divide
value by 100 when propertyDescriptor has a “PercentageAttribute”
But when you’ll get value from the database, it will be divided by 100… So if your users can edit these data, you’ll have to multiply by 100 before passing it to the “edit” view…
I think ViewModels with a little bit of logic, or transient properties in your POCOs, or whatever else would be easier !
Hard to say without understanding WHY you really need this.