Bools的数据注释

时间:2014-01-30 03:21:51

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

下面我有三个不同的类别。

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

//残疾

    [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; }

1 个答案:

答案 0 :(得分:4)

您可能需要考虑使用其他型号。如果您要执行的操作是每个类别至少强制执行一次选择,那么最好将它们组合在一起并使用必需的属性。

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

然后在你的模型上有一个属性:

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