Asp MVC unobtrusive Client Validation always returning true
Asked Answered
L

1

6

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?

Lanceted answered 19/5, 2011 at 16:40 Comment(0)
S
11

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.

Spaulding answered 19/5, 2011 at 16:45 Comment(6)
after having spent a day on this finally found the soulution. I also use tab views and load the tab view with ajax request. The only thing doesnt work now is the colour of the text that appear as error. It is normally red but on the loaded html it shows black. any idea and how to fix this ?Deck
@akdurmus sounds like the CSS for validation hasn't been loaded into the pageSpaulding
First of all thanks for coming back. As you mentioned that by using firebug I identified the issue that the CSS has not been loaded. But on the other pages it seems to function correctly. This is a partial view loaded from the server. What would be the solution to this issue? Attaching the source file to this partial view? ThanksDeck
@akdurmus I would think that the layout would be the right place for it. You might want to just include those classes in your standard page CSS.Spaulding
it took me hours to work out why the #¤&% validation didn't work, but this answer made me realize why. I am showing a partial view returned from an ajax request and have to run this command to initialize the validation for the new HMTL. Thanks!Buchalter
This is also required when you need to invoke a partial view to render the code for a bootstrap modal. Works like a charm :)Spoliate

© 2022 - 2024 — McMap. All rights reserved.