What to consider:
- Data annotations will not be displayed unless
FormContext exists in any method that you use to create this optional object. If you are using a partial view, add the following to it:
-
if (this.ViewContext.FormContext == null) { this.ViewContext.FormContext = new FormContext(); }
- If you dynamically add an element to the page via AJAX, after adding a new element, you should clear the validation data in the DOM and reanalyze all your validation elements, for example:
-
$("form").removeData("validator"); $("form").removeData("unobtrusiveValidation"); $.validator.unobtrusive.parse("form");
counsellorben
source share