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,這是我的業務規則。 爲此,編寫了下面的定製校驗器。
這是自定義驗證我已經寫:
[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
}
請幫我...