简体   繁体   English

ASP.NET MVC 4 Colorbox部分视图表单未验证

[英]ASP.NET MVC 4 Colorbox Partial View Form Not Validating

I have an ASP.NET MVC 4 application that is using unobtrusive jQuery validation for form inputs. 我有一个ASP.NET MVC 4应用程序,它在表单输入中使用了简洁的jQuery验证。 This works fine on all normal views. 在所有普通视图上都可以正常工作。 When I load pages into a lightbox using Colorbox, the forms are posting back to the controller to run the validation without running any of the validation on the client side. 当我使用Colorbox将页面加载到灯箱中时,表单将回发到控制器以运行验证,而无需在客户端上运行任何验证。 I have user other lightbox APIs besides Colorbox without any issues. 除了Colorbox之外,我还有其他lightbox API用户,没有任何问题。

By tinkering around, it seems like the jQuery validation is not setup against this form, which would make sense since the form is loaded after page loads (when the user clicks a link). 通过修改,似乎没有针对此表单设置jQuery验证,这是有道理的,因为在页面加载后(用户单击链接时)加载了表单。 I call validate() against the form and only the extra validation methods I wrote run, not any of the MVC validations methods (like Required or RegularExpression). 我针对表单调用validate(),并且只运行我编写的其他验证方法,而不调用任何MVC验证方法(例如Required或RegularExpression)。 Plus the form still posts back to the server even if there are form errors. 另外,即使存在表单错误,该表单仍会回发到服务器。

What am I missing? 我想念什么?

Unobtrusive validation inits on page load and when you load partial with ajax, you need to init your form: 页面加载时的非侵入式验证init,当您使用ajax加载局部时,您需要初始化表单:

$.validator.unobtrusive.parse('#form_selector');

More details: jquery.validate.unobtrusive not working with dynamic injected elements 更多详细信息: jquery.validate.unobtrusive不适用于动态注入的元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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