PLEASE NOTE: I've answered my own question with a link to an answer to a similar question. I'll accept that answer once I'm allowed to (unless anyone comes up with a better answer meantime).
I hav开发者_StackOverflow社区e a database column defined as NVARCHAR(1000) NOT NULL DEFAULT(N'')
- in other words, a non-nullable text column with a default value of blank.
I have a model class generated by the Linq-to-SQL Classes designer, which correctly identifies the property as not nullable.
I have a TextAreaFor
in my view for that property. I'm using UpdateModel
in my controller to fetch the value from the form and populate the model object.
If I view the web page and leave the text area blank, UpdateModel
insists on setting the property to NULL
instead of empty string. (Even if I set the value to blank in code prior to calling UpdateModel
, it still overwrites that with NULL
). Which, of course, causes the subsequent database update to fail.
I could check all such properties for NULL
after calling UpdateModel
, but that seems ridiculous - surely there must be a better way?
Please don't tell me I need a custom model binder for such a simple scenario...!
Might be duplicate or something in the line of this:
MVC binding form data problem
I fear custom model binder will be necessary. ;)
You might want to use a partial class implementation of your entity that implements the on property changed handler for that particular property. When you detect that the property has been changed to NULL, simply change it to string.Empty
. That way anytime NULL is assigned to the property it gets reset to the empty string.
精彩评论