manual mvc4 data annotation model validation

62 views Asked by At

I need to validate viewmodel manually.But I am getting null result after calling validate method on ModelValidator object.

 var temp = validators.First().Validate(basicDetails);

Is there any work around to validate manually created viewmodel?

public static ModelStateDictionary ValidateDataLead(LeadViewModel newLead, ControllerContext context)
    {
        var basicDetails = new BasicDetailsViewModel();
        basicDetails.Name = new FullNameViewModel();
        var modelState = new ModelStateDictionary();

        var types = basicDetails.GetType().GetProperties();
        foreach (PropertyInfo property in types)
        {
            var propertyMetaData = ModelMetadataProviders.Current.GetMetadataForProperty(() => basicDetails, basicDetails.GetType(), property.Name);
            var validationProvider = ModelValidatorProviders.Providers[0];
            var validators = validationProvider.GetValidators(propertyMetaData, context);
        }


        return modelState;
    }

Any help would be appreciated.

0

There are 0 answers