We have a partial view that contains a form with unobtrusive client validation enabled.
If we load the partial view using Html.Action, the validation works on the client side.
If when the user clicks a link we use JQuery to populate a div with the partial view , the client validation always returns true.
Any idea what is going on?
Unobtrusive Validation allows us to take the already-existing validation attributes and use them client-side to make our user experience that much nicer. The Unobtrusive script files are included automatically with new MVC projects in Visual Studio, but if you don't have them you can get them from NuGet.
Unobtrusive validation makes use of jQuery library and data-* attributes of HTML5 to validate data entered in the web form controls. Unobtrusive validations can be enabled in the web. config file, Global. asax or individual Web Form code-behind.
validator. unobtrusive. parse(selector) method to force parsing. This method parses all the HTML elements in the specified selector and looks for input elements decorated with the [data-val=true] attribute value and enables validation according to the data-val-* attribute values.
Model validation is the process of checking whether the user input is suitable for model binding and if not it should provide useful error messages to the user. The first part is to ensure that only valid entries are made. This should filter inputs which don't make any sense.
You need to parse the new html to hook up the validation controls. You can do this using:
$.validator.unobtrusive.parse( $('.selector' ) );
where the selector returns the container holding the new HTML. This is what I use with tabbed interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With