1

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

Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • Also, I don't know how does it look to designate a string type "not null", that can't be possible, string is reference type, it can be null. What would it be if not null? Empty string? Sure, string.empty is one option but the Framework does not create empty strings by default when you do, for instance, string MyString; – mare Apr 06 '10 at 21:24
  • @mare: since I'm using the VS2008 "Linq to SQL Classes" designer to create my model class, it has a UI that allows me to view the classes in a "database diagram"-like view. Through that, I can inspect the properties of the various tables & columns, and that's where I can see that it understands that it's a non-nullable column. I don't see why that can't be reflected in the generated class (and indeed I can't see why the default value can't be reflected there also!). – Gary McGill Apr 06 '10 at 21:28
  • It would have to implicitly know that when a non nullable SQL column appears replace it's default values with string.Empty. I understand this could be useful in your case and many other cases but apparently (unfortunately) it does not work like that. – mare Apr 06 '10 at 21:35

2 Answers2

2

Might be duplicate or something in the line of this:

MVC binding form data problem

I fear custom model binder will be necessary. ;)

Community
  • 1
  • 1
mare
  • 13,033
  • 24
  • 102
  • 191
0

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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • @tvanfosson: I could, but then I'd have to repeat the same code dozens of times (and worse - I'd probably be creating the partial classes just to do that in many cases). See my own answer with a link to a much cleaner solution. – Gary McGill Apr 06 '10 at 21:53
  • if that's the behavior you always want, I suppose that would do the trick. It has the inverse problem of storing empty strings, though, if the column is nullable. – tvanfosson Apr 06 '10 at 22:09
  • true, if you mean changing the default binder - but the same answer also describes how to do this on a per-property basis using data annotations. – Gary McGill Apr 06 '10 at 22:44