Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

javascript - Using MVC3 and JQuery to dynamically and mantain the DataAnnotations

I have a problem similar to this: Using MVC and JQuery to dynamically & consistently add values to a table , but the difference is:

I have table and I'm cloning the last row (to keep the same format) with:

var $tableBody = $('#record').find("tbody");
$trLast = $tableBody.find("tr:last");
$trNew = $trLast.clone();
$trLast.after($trNew);

`

The rows are created succesfully, but the validations are binded to the first row (inputs, selects and textarea)

Inspecting the elment, are displayed:

<td>
// Code generated with the jquery .clone()
    <input data-val="true" data-val-required="Es necesario agregar una fecha." id="Salidas_0__Fecha" name="Salidas[1].Fecha" type="datetime" value="" autocomplete="off" class="hasDatepicker input-validation-error">
    <span class="field-validation-valid" data-valmsg-for="Salidas[1].Fecha" data-valmsg-replace="true"></span>
</td>

But the field-validation-valid is not binded to the input. Exist a way to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, when you play with dynamic content, you have to add JQuery Validation to the new content after cloning the row:

$.validator.unobtrusive.parse($("form"));

Second, the attribute "data-valmsg-for", on spans with "field-validation-valid" class, must be the same of attribute "name" of his corresponding input in each row of your table.

So, for example, a table with three rows has to look like this:

<td>
    <input data-val="true" data-val-required="Es necesario agregar una fecha." id="Salidas_0__Fecha" name="Salidas[0].Fecha" type="datetime" value="" autocomplete="off" class="hasDatepicker input-validation-error">
    <span class="field-validation-valid" data-valmsg-for="Salidas[0].Fecha" data-valmsg-replace="true"></span>
</td>
<td>
    <input data-val="true" data-val-required="Es necesario agregar una fecha." id="Salidas_1__Fecha" name="Salidas[1].Fecha" type="datetime" value="" autocomplete="off" class="hasDatepicker input-validation-error">
    <span class="field-validation-valid" data-valmsg-for="Salidas[1].Fecha" data-valmsg-replace="true"></span>
</td>
<td>
    <input data-val="true" data-val-required="Es necesario agregar una fecha." id="Salidas_2__Fecha" name="Salidas[2].Fecha" type="datetime" value="" autocomplete="off" class="hasDatepicker input-validation-error">
    <span class="field-validation-valid" data-valmsg-for="Salidas[2].Fecha" data-valmsg-replace="true"></span>
</td>

And the last is also needed for the correct Model Binding when posting the form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...