简体   繁体   中英

ViewModel Validation and jquery Ajax

I have a strongly type View in an ASP.NET MVC2 application in which i load a dropdownlist after its parent is selected using jquery ajax.The issue i am facing is that after the dropdownlist gets loaded the validation of the View stops working. I am using data-annotations for validating the Views?

This most likely has to do with DOM replacement. When you load your additional data, do you replace any of the existing DOM of your form? That would also remove any events bound to previous elements.

After receiving the data from the Ajax call and then subsequently replacing the Dom element, you need to re-parse the document with jquery unobtrusive validation like so:

$.validator.unobtrusive.parse("#dropdownId")

obviously replacing dropdownId with the actual Id of your dropdown. if you're still having issues with other validation on the page, instead of parsing the dropdown, parse the entire document holder, eg say you have everything in a div with the id of main, you'd do:

$.validator.unobtrusive.parse("#main")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM