I realise this question has been asked before but I am still struggling with validating this checkbox using data annotations. What am I missing. As it is right now the checkbox is not validated
view model
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value;
}
}
[Display(Name = "I accept the terms and conditions")]
[Required]
[MustBeTrue(ErrorMessage = "Please accept terms and conditions")]
public bool TermsConditions { get; set; }
Razor and html
<div class="editor-label">
@Html.LabelFor(model => model.TermsConditions)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.TermsConditions, new { id = "chkTermsAndConditions" })
Yes
</div>
@Html.ValidationMessageFor(model => model.TermsConditions)