Reputation: 11796
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
Upvotes: 5
Views: 1884
Reputation: 28611
Old question, but in MVC4 you can add this to your POCO property:
[DisplayFormat(ConvertEmptyStringToNull = false)]
Upvotes: 11
Reputation: 5124
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.
Upvotes: 5