与BOOLS数据注解 [英] Data Annotation with Bools

查看:154
本文介绍了与BOOLS数据注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我将如何构建验证,以确保至少有一个Boolean获取每个类别中选择?

//残疾人

  [显示(NAME =学习障碍)]
    公共BOOL LD {搞定;组; }    [显示(NAME =发育障碍)]
    公共BOOL DD {搞定;组; }    [显示(NAME =AD / HD)]
    公共BOOL多动症{搞定;组; }    [显示(NAME =自闭症)]
    公共BOOL自闭症{搞定;组; }

//年龄组

  [显示(NAME =孩子)
    公共BOOL孩子{搞定;组; }    [显示(名称=青春)]
    公共BOOL青年{搞定;组; }    [显示(NAME =成人)]
    公共BOOL成人{搞定;组; }

//策略类型

  [显示(NAME =学术)]
    公共BOOL学术{搞定;组; }    [显示(NAME =行为)]
    公共BOOL行为{搞定;组; }    [显示(NAME =通信)]
    公共BOOL通信{搞定;组; }    [显示(NAME =社会)]
    公共BOOL社会{搞定;组; }


解决方案

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

 公开枚举年龄
{
  [显示(NAME =孩子)
  儿童,
  [显示(名称=青春)
  青年,
  [显示(NAME =成人)
  成人
}

然后让你的模型的属性,像这样:

  [必需]
公共年龄MyAge {搞定;组; }

Down below I have three different categories.

How would I structure the validation to make sure that at least one boolean gets selected per category?

//Disabilities

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

//Age Group

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

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

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

//Strategy Type

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

解决方案

You may want to consider using a different model. If what you are trying to do is enforce at least one selection per category then it may be better to group them together and use a required attribute.

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

Then have a property on your model like so:

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

这篇关于与BOOLS数据注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆