How would you validate a checkbox in ASP.Net MVC 2?

前端 未结 4 1632
庸人自扰
庸人自扰 2021-01-05 03:09

Using MVC2, I have a simple ViewModel that contains a bool field that is rendered on the view as a checkbox. I would like to validate that the user checked the box. The [R

4条回答
  •  Happy的楠姐
    2021-01-05 04:08

    a custom validator is the way to go. I'll post my code which I used to validate that the user accepts the terms ...

    public class BooleanRequiredToBeTrueAttribute : RequiredAttribute
    {
        public override bool IsValid(object value)
        {
            return value != null && (bool)value;
        }
    }
    

提交回复
热议问题