手动mvc4数据注释模型验证

时间:2015-06-04 14:05:32

标签: c# asp.net-mvc-4 data-annotations unobtrusive-validation

我需要手动验证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;
    }

任何帮助都将不胜感激。

0 个答案:

没有答案