2014-01-30 65 views
1

下面我有三个不同的类别。带Bools的数据注释

我将如何构造验证以确保每个类别至少选择一个布尔值?

//残疾人

[Display(Name = "Learning Disabilities")] 
    public bool LD { get; set; } 

    [Display(Name = "Developmental Disabilities")] 
    public bool DD { get; set; } 

    [Display(Name = "AD/HD")] 
    public bool ADHD { get; set; } 

    [Display(Name = "Autism")] 
    public bool Autism { get; set; } 

//年龄组

[Display(Name = "Child")] 
    public bool child { get; set; } 

    [Display(Name = "Youth")] 
    public bool youth { get; set; } 

    [Display(Name = "Adult")] 
    public bool adult { get; set; } 

//策略类型

[Display(Name = "Academic")] 
    public bool academic { get; set; } 

    [Display(Name = "Behaviour")] 
    public bool behaviour { get; set; } 

    [Display(Name = "Communication")] 
    public bool communication { get; set; } 

    [Display(Name = "Social")] 
    public bool social { get; set; } 
+0

可能重复。如何设置验证,要求两个字段之一取决于模型的布尔值?](http://stackoverflow.com/questions/11143151/asp-net-mvc3-how-to-set-validation-to-require-之一的两字段-取决于-ON-BO) – MarcinJuraszek

回答

4

你可能要考虑使用不同的模型。如果您要做的是每个类别至少执行一次选择,那么将它们组合在一起并使用必需的属性可能会更好。

public enum Age 
{ 
    [Display(Name="Child") 
    Child, 
    [Display(Name="Youth") 
    Youth, 
    [Display(Name="Adult") 
    Adult 
} 

然后有像这样的模型中的一个属性:[ASP.NET MVC3的

[Required] 
public Age MyAge { get; set; }