2014-01-30 64 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; }