I'm using Entity Framework Code-First with POCOs for my database interaction. I have a field that is optional called "Title".
When I get the object from the form, the default model binder automagically makes "Title" null instead of empty.
How do I get the model binder to return an empty string instead of null?
Thanks for any help
Old question, but in MVC4 you can add this to your POCO property:
[DisplayFormat(ConvertEmptyStringToNull = false)]
You are requesting non-standard behaviour, so you need non-standard solution. Extend default model binder, override its CreateModel method - do what you want for specific cases, and return base.CreateModel for others.
Dont forget to register it as default model binder at application start.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With