我需要手动验证viewmodel。但是我在ModelValidator对象上调用validate方法后得到null结果。
var temp = validators.First().Validate(basicDetails);
是否有任何解决方法来验证手动创建的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;
}
任何帮助都将不胜感激。