Thomas
Thomas

Reputation: 34188

ASP.Net MVC datettime compare with nullable date time

when my model property is declared with not null able date-time then no problem occur when we check data type of property but the moment i have a null able date time property then the below code return false

if (datatype.GetType().Equals(new DateTime().GetType()))
{}

here is full code

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    ValidationResult validationResult = ValidationResult.Success;
    try
    {
        // Using reflection we can get a reference to the other date property, in this example the project start date
        var containerType = validationContext.ObjectInstance.GetType();
        var field = containerType.GetProperty(this.otherPropertyName);
        var extensionValue = field.GetValue(validationContext.ObjectInstance, null);
        var datatype = extensionValue.GetType();

        //var otherPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(this.otherPropertyName);
        if (field == null)
            return new ValidationResult(String.Format("Unknown property: {0}.", otherPropertyName));
        // Let's check that otherProperty is of type DateTime as we expect it to be
        if (datatype.GetType().Equals(new DateTime().GetType()))
        {
            DateTime toValidate = (DateTime)value;
            DateTime referenceProperty = (DateTime)field.GetValue(validationContext.ObjectInstance, null);
            // if the end date is lower than the start date, than the validationResult will be set to false and return
            // a properly formatted error message
            if (toValidate.CompareTo(referenceProperty) < 1)
            {
                validationResult = new ValidationResult(ErrorMessageString);
            }
        }
        else
        {
            validationResult = new ValidationResult("An error occurred while validating the property. OtherProperty is not of type DateTime");
        }
    }
    catch (Exception ex)
    {
        // Do stuff, i.e. log the exception
        // Let it go through the upper levels, something bad happened
        throw ex;
    }

    return validationResult;
}

get me the idea of best fix because my property should be null able date time fields. thanks

Upvotes: 0

Views: 193

Answers (3)

Igor
Igor

Reputation: 62228

Your variable field (returned by call GetProperty) is type PropertyInfo so if you want the type withotu going to the value you need to call property PropertyType.

if (field.PropertyType == typeof(DateTime) || (field.PropertyType.IsGenericType && field.PropertyType == typeof(Nullable<DateTime>))) { }

Here is the reference for PropertyInfo.

You should convert your type checks to typeof(TypeHere) instead of new TypeHere.GetType(). The former does not have to create a new instance which could be expensive if the code is repeated / looped many times. See my example.

Also take @David's (the other answer) advice about using throw; instead of throw ex;. The later will reset the entire captured exception call stack.

Upvotes: 2

Brian Herbert
Brian Herbert

Reputation: 1191

You cannot check the type of a null value therefore you need to check if it's null before you do anything else.

if(datatype == null)
            validationResult = new ValidationResult(ErrorMessageString);        
else if (datatype.GetType() == typeof(DateTime))
{
    if(!IsValidDate(datatype.Value))
            validationResult = new ValidationResult(ErrorMessageString); 
    ...
}

Upvotes: 1

David
David

Reputation: 218876

It's not really clear why you're comparing the types and not the values, but I guess that's beside the point. In this comparison:

datatype.GetType().Equals(new DateTime().GetType())

This is going to return true if datatype is of type DateTime. So, reasonably, it will not return true if datatype is of some other type, such as Nullable<DateTime>.

You should be able to compare with that type instead:

datatype.GetType().Equals(new Nullable<DateTime>().GetType())

Or perhaps with both:

datatype.GetType().Equals(new DateTime().GetType()) ||
datatype.GetType().Equals(new Nullable<DateTime>().GetType())

Side note: Your catch block is entirely superfluous and is actually removing information about the exception. It's something of an anti-pattern and should be removed entirely. If you are actually handling the exception in any way in that catch block and do want to re-throw, just use throw; instead of throw ex;.

Upvotes: 1

Related Questions