Question

I have from with conditional content, according with conditions using jQuery i am including or excluding this parts from the form elements.

Here is my functions:

function MoveInsideForm(id) {
       $("#" + id).insertAfter("#myForm")
   }

function MoveOutsideForm() {
       $("#myPartial1").insertAfter("#element-outside-from");
       $("#myPartial2").insertAfter("##element-outside-from");
   }

The problem is with insertAfter() it is does not getting copied HTML 5 custom attributes

For example i have an element like that

<input data-val="true" data-val-required="*" id="MyInput" name="MyInput" type="text" value="" class="input-validation-error"/>

But insertAfter() copying it like that:

<input id="MyInput" name="MyInput" type="text" value=""/>

Is there any way i can say to insertAfter() to copy HTML 5 attributes as well? My jQuery version is 1.6.1.

UPDATE:

Thanks guys for comments. Here is the thing, when i am rendering my partials inside the form the inputs getting generated with unobtrusive data attributes, but if i am rendering my partials outside the form, the unobtrusive data attributes initially not getting included in to inputs.

So when i am rendered the partials outside the form they initially does not contain data attributes. So it is not issue with jQuery insertAfter(), is it the nature of unobtrusive data validation attributes generation?

Was it helpful?

Solution

In order to generate unobtrusive validation, MVC must have a FormContext. When you place an HtmlHelper outside of Html.BeginForm or Ajax.BeginForm, the HtmlHelpers will not have unobtrusive validation attributes, unless you manually instantiate a FormContext. You can manually instantiate a FormContext by inserting the following code before your helpers:

this.ViewContext.FormContext = new FormContext();

If you are placing your helpers before a BeginForm(), then be sure to clear the FormContext after your helpers and before beginning the form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top