Asp.Net MVC does automatic model validation for DateTime but no others

Posted by MattSlay on Stack Overflow See other posts from Stack Overflow or by MattSlay
Published on 2010-03-15T04:50:08Z Indexed on 2010/03/15 4:59 UTC
Read the original article Hit count: 524

Filed under:

I'm using MVC 2 with some Models from a LinqToSql project that I built. I see that when I post back to a Controller Action after editing a form that has a DateTime field from the Model, the MVC Html.ValidationMessageFor() helper will nicely display an error beside the Date text box.

This seems to happen automatically when the you test ModelState.IsValid() in the Controller Action, as if the MVC model binding automatically knows that the DateTime field cannot be empty. My question is... I have some other string fields in these LinqToSql generated classes that are Not-Nullable (marked as Not Nullable in Sql Server which passes thourgh to the LinqToSql generated classes), so why doesn't Mr. MVC pick up on those as well and display a "Required" message in the ValidationMessageFor() placeholders I have added for those fields?

Sure, I have successfully added the MetadataType(typeof) buddy classes to cover these Non-nullable string fields, but it sure does seem redundant to add all this metadata in buddy classes when the LinqToSql generated classes already contain enough info that MVC could sniff out. It MVC validation works with DateTime automatically, why not these Not-nullable fields too?

© Stack Overflow or respective owner

Related posts about asp.net-mvc