Thursday, 5 September 2013

RequiredIf Attribute Check if value is HttpPostedFileBase

RequiredIf Attribute Check if value is HttpPostedFileBase

Could someone help me in my code to check if the value variable is a
HttpPostedFileBase type then get it's Property FileName something like
value = value.GetType().GetProperty("FileName") but I can't seem to do
this with an IF Statement. it says not an instance to an object
i've tried
if(value.GetType() is HttpPostedFileBase) {
value = value.GetType().GetProperty("FileName");
}
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var containerType = validationContext.ObjectInstance.GetType();
var field = containerType.GetProperty(DependentProperty);
if (field != null)
{
var dependentValue =
field.GetValue(validationContext.ObjectInstance, null);
if (dependentValue != null && dependentValue is string)
{
dependentValue = (dependentValue as string).Trim();
if (!AllowEmptyStrings && (dependentValue as
string).Length == 0)
{
dependentValue = null;
}
}
if((dependentValue == null && TargetValue == null) ||
(dependentValue != null && (TargetValue.Equals("*") ||
dependentValue.Equals(TargetValue))))
{
if (!_innerAttribute.IsValid(value))
return new
ValidationResult(FormatErrorMessage(validationContext.DisplayName),
new[] { validationContext.MemberName });
}
}
return ValidationResult.Success;
}

No comments:

Post a Comment