ASP.NET MVC 3 unobtrusive jQuery client-side validation with child collections

心不动则不痛 提交于 2019-12-04 14:38:31

The problem you are experiencing is that no FormContext exists when the server is creating your new row. A FormContext must exist for unobtrusive validation attributes to be added to the generated HTML. Add the following to the top of your partial view:

if (this.ViewContext.FormContext == null) 
{
    this.ViewContext.FormContext = new FormContext(); 
}

counsellorben

As an answer to the second part of my question, apparantly you can't call jquery.validate.unobtrusive.parse on a form twice. If the form already has a validator, it is not replaced.

Going to give accepted answer to counsellorben because his bit was by far the hard bit :D

EDIT

Ooh, seems I wasn't the first to find it either:

http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/

Add unobstrusive like these lines:

1) $.validator.unobtrusive.parse($('#yourformid'));

2) var $form1 = $('#workOrderDetails');

3) if ($form1.valid()) {  //your ajax or your code comes here }

I assume that you have added validation summary or validationmessagefor for messages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!