ASP.NET MVC 2 RC2 Model Binding with NVARCHAR NOT NULL column

Posted by Gary McGill on Stack Overflow See other posts from Stack Overflow or by Gary McGill
Published on 2010-04-06T21:16:21Z Indexed on 2010/04/06 21:23 UTC
Read the original article Hit count: 499

I have 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...!

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-mvc