public class BooleanRequiredAttribute : ValidationAttribute, IClientValidatable
{
public bool Value { get; set; }
public override bool IsValid(object value) { res = value != null && Value; return res; }
public IEnumerable GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule rule = new ModelClientValidationRule
{
ValidationType = "brequired",
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
};
rule.ValidationParameters.Add("br", res);
yield return rule;
}
public bool res { get; set; }
}
//布尔类型必勾选
$.validator.addMethod("brequired", function (value, element, param) {
return element.checked;
});
$.validator.unobtrusive.adapters.addSingleVal("brequired", "br");
[BooleanRequired]
[Display(Name = "必选同意")]
public bool Agree { get; set; }
"showclause" href="Javascript:void(0);">网站服务条款
@Html.CheckBoxFor(m => m.Agree)同意
@Html.ValidationMessageFor(m => m.Agree)
感谢阅读.....