c# - ASP.NET MVC: Why Range Data Annotation is not working on checkbox in client side? -
c# - ASP.NET MVC: Why Range Data Annotation is not working on checkbox in client side? -
i have form fields required
info annotation , have "accept terms , conditions" checkbox info annotation [range(typeof(bool), "true", "true", errormessage="you must take terms , conditions proceed")]
works fine thing annoying required
info annotation fires errors before post.. mean, click on submit , form not create post , shows errors range, form post , shows errors.
why that? mutual behavior?
i came solution:
public class enforcetrueattribute : validationattribute, iclientvalidatable { public override bool isvalid(object value) { if (value == null) homecoming false; if (value.gettype() != typeof(bool)) throw new invalidoperationexception("can used on boolean properties."); homecoming (bool)value == true; } public override string formaterrormessage(string name) { homecoming "the " + name + " field must checked in order continue."; } public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { yield homecoming new modelclientvalidationrule { errormessage = string.isnullorempty(errormessage) ? formaterrormessage(metadata.displayname) : errormessage, validationtype = "enforcetrue" }; } }
and in js:
jquery.validator.addmethod("enforcetrue", function (value, element, param) { homecoming element.checked; }); jquery.validator.unobtrusive.adapters.addbool("enforcetrue");
c# asp.net-mvc asp.net-mvc-4 data-annotations
Comments
Post a Comment