2011-08-09 11 views
1

開発用Net MVC 3。 今コレクションのプロパティでカスタムバリデーターを作成する必要があるシナリオがあります。 私が直面している問題は、コレクションの長さが1で、コレクションに複数の要素があるときにこのカスタムバリデータがうまく動作するということです。カスタム検証はエラーをプッシュしますが、 ModelStateでエラーがプラグインされていません。この場合、私は何をすべきですか?私は以下のコードを貼り付けています。 私は、コレクション内の必要なエンティティの最小数を検証する必要がある - :問題コレクションプロパティでカスタムバリデータを作成する

public class CreateTestModel 
{ 
    [Required] 
    public string Name { get; set; } 

    public string Description { get; set; } 

    public RadioButtonListViewModel<GoalTypes> Goals { get; set; } 

    [MinimumRequired("IncludedEntities", "ExcludedEntities", 1, ErrorMessage = "1 Entity is compulsory")] 
    public IEnumerable<TestEntityModel> IncludedEntities { get; set; } 

    public IEnumerable<TestEntityModel> ExcludedEntities { get; set; } 

    public IEnumerable<TestFilterModel> IncludedFilters { get; set; } 

    public IEnumerable<TestFilterModel> ExcludedFilters { get; set; } 

    public IEnumerable<BucketModel> Buckets { get; set; } 

    public bool AutoDecision { get; set; } 

    public DateTime StartDate { get; set; } 

    public DateTime EndDate { get; set; } 

    public int AdminId { get; set; } 

    [DefaultValue(true)] 
    public bool IsEnabled { get; set; } 
    } 

カスタムバリデータの目的:

これは私が検証するモデルです。 例:この場合、IncludedEntitiesとExcludedEntitiesに両方のプロパティを含む最小数1が必要なシナリオがあります。これが私のビジネスルールです。 このため、カスタムValidator以下が記述されています。

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
public class MinimumRequired : ValidationAttribute,IClientValidatable 
{ 
    public int numberOfMandatoryEntities{get; private set;} 
    public int totalCountofIncludeEntities { get; private set; } 
    public bool isBucket { get; set; } 
    public string Property1{get; private set;} 
    public string Property2{ get; private set; } 
    private const string DefaultErrorMessageFormatString = "Atleast one entity is required"; 

    public MinimumRequired(string Property1, string Property2, int MandatoryCount) 
    { 
     this.Property1 = Property1; 
     if (!String.IsNullOrEmpty(Property2)) 
      this.Property2 = Property2; 
     numberOfMandatoryEntities = MandatoryCount; 
    } 


    public override string FormatErrorMessage(string name) 
    { 
     return string.Format(ErrorMessageString, name); 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     object property2Value = null; 
     object property1Value = null; 
     int property1Count=0; 
     if(!String.IsNullOrEmpty(Property2)) 
     property2Value = validationContext.ObjectInstance.GetType().GetProperty(Property2).GetValue(validationContext.ObjectInstance, null); 
     property1Value = validationContext.ObjectInstance.GetType().GetProperty(Property1).GetValue(validationContext.ObjectInstance, null); 
     if (property1Value != null) 
     { 
     property1Count = ((IEnumerable<Object>)property1Value).Count(); 
     } 

     if (property2Value != null) 
     { 
      property1Count = property1Count + ((IEnumerable<Object>)property2Value).Count(); 
     } 
     if (property1Count < numberOfMandatoryEntities) 
     { 
      return new ValidationResult(ErrorMessage); 
     } 

     return ValidationResult.Success; 
    } 
    #region IClientValidatable Members 

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
    { 
     var x = new ModelClientValidationRule(); 
     x.ValidationType = "entityvalidator"; 
     x.ErrorMessage = string.Format(ErrorMessageString, metadata.GetDisplayName()); 
     x.ValidationParameters.Add("mandatoryentity", numberOfMandatoryEntities); 
     return new[] 
     { 
      x 
     }; 
    } 

    #endregion 



} 

を私を助けてください...

答えて

0

を、私はそれが私のために完璧に動作上記のコードをtriend:

これは私が書かれているカスタムバリデータです。

関連する問題