Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp MVC unobtrusive Client Validation always returning true

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?

like image 275
Kenoyer130 Avatar asked May 19 '11 16:05

Kenoyer130


People also ask

What is unobtrusive validation in ASP.NET MVC?

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.

What is unobtrusive validation in asp net?

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.

What is validator unobtrusive parse?

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.

How does MVC validation work?

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.


1 Answers

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.

like image 131
tvanfosson Avatar answered Oct 11 '22 10:10

tvanfosson