DataAnnotations Automatic Handling of int is Causing a Roadblock

Posted by DM on Stack Overflow See other posts from Stack Overflow or by DM
Published on 2010-06-02T16:18:51Z Indexed on 2010/06/02 19:44 UTC
Read the original article Hit count: 641

Summary: DataAnnotation's automatic handling of an "int?" is making me rethink using them at all.

Maybe I'm missing something and an easy fix but I can't get DataAnnotations to cooperate. I have a public property with my own custom validation attribute:

[MustBeNumeric(ErrorMessage = "Must be a number")]
public int? Weight { get; set; }

The point of the custom validation attribute is do a quick check to see if the input is numeric and display an appropriate error message. The problem is that when DataAnnotations tries to bind a string to the int? is automatically doesn't validate and displays a "The value 'asdf' is not valid for Weight."

For the life of me I can't get DataAnnotations to stop handling that so I can take care of it in my custom attribute.

This seems like it would be a popular scenario (to validate that the input in numeric) and I'm guessing there's an easy solution but I didn't find it anywhere.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc