views:

287

answers:

1
public class Dinner
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public Category Category { get; set; }
        public DateTime? DateCreated { get; set; }
    }

Model view for that class (important part) is

public class DinnerModelView
    {
        ...
        [UIHint("DatePicker")]
        [DateTime(ErrorMessage = "Invalida date")]
        public DateTime? DateCreated { get; set; }
    }

Where DateTimeAttriburte is

public class DateTimeAttribute : ValidationAttribute
    {
        public DateTimeAttribute () : base (() => "Invalid date") { }
        public DateTimeAttribute(string errorMessage) : base(() => errorMessage) { } 
        public override bool IsValid(object value)
        {
            if (value == null)
                return true;

            bool isValid = false;
            if (value is DateTime)
                isValid = true;

            DateTime tmp;
            if (value is String)
            {
                if(String.IsNullOrEmpty((string)value))
                    isValid = true;
                else
                    isValid = DateTime.TryParse((string)value, out tmp);
            }

            return isValid;
        }
    }

However model state error still says "The value 'xxxx' is not valid for DateCreated." I am not able to replace this message. WHY?

A: 

Use protected ValidationAttribute(string errorMessage) instead of protected ValidationAttribute(System.Func errorMessageAccessor). The later on is for access a string defined in a resource file. Check out http://msdn.microsoft.com/en-us/library/cc679238.aspx

Sperling