0

This is my Model class

public class Model
{
    [DataType(DataType.DateTime, ErrorMessage = "My error message")]
    public DateTime Day { get; set; }
}

When I try to input incorrect value for example "notdate" i got the error "The value 'notdate' is not valid for Day." instead of my specified ErrorMessage "My error message".

I use ASP.NET MVC 3.0 beta. This is a bug?

2 Answers 2

1

There are a few things to note about the behavior you are describing.

First, you are receiving this error because an exception is being thrown when attempting to assign the string value 'notdate' to a DateTime field. When this happens, any validation messages that may have been associated with the field will be overwritten with the generic message: The value '{0}' is not valid for {1}.

Second,the base DataTypeAttribute doesn't actually perform any validation on the field. Using Reflector, you will see that the DataTypeAttribute.IsValid() method is declared as follows:

public override bool IsValid(object value)
{
    return true;
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

No, this is the default functionality of the existing model binder.

The DataType has nothing to do with basic model binding and won't override basic model binding errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.