使用MVC3和JQuery动态并维护DataAnnotations [英] 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);
`
行创建成功,但是验证绑定到第一行(inputs
,selects
和textarea
)
The rows are created succesfully, but the validations are binded to the first row (inputs
, selects
and textarea
)
显示正在检查的要素:
<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>
但是field-validation-valid
未绑定到input
.
存在做到这一点的方法吗?
But the field-validation-valid
is not binded to the input
.
Exist a way to do that?
推荐答案
首先,在处理动态内容时,必须在克隆行后向新内容添加JQuery验证:
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"));
第二,在具有"field-validation-valid"类的跨度上,属性"data-valmsg-for"必须与表的每一行中相应输入的属性"name"相同.
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.
这篇关于使用MVC3和JQuery动态并维护DataAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!