You have a dynamic table with textboxes, and you want to apply class validation to all these textboxes. jQuery's unobtrusive validation is not registering the added textboxes, resulting in the validation not working.
To resolve this, you need to:
@for (int i = 0; i < Model.TargetInfo.Count; i++) { <tr> <td> @Html.TextBoxFor(m => m.TargetInfo[i].TargetColor_U, new { id = "", @class = "form-control" }) @Html.ValidationMessageFor(m => m.TargetInfo[i].TargetColor_U) <input type="hidden" name="TargetInfo.Index" value=@i /> </td> <!-- Other columns with similar markup --> </tr> }
var form = $('form'); var newrow = $('#newrow'); var tablebody = $('#tablebody'); $("#btnAddTarget").click(function() { var index = (new Date()).getTime(); var clone = newrow.clone(); clone.html($(clone).html().replace(/#/g, index)); var row = clone.find('tr'); tablebody.append(row); // Reparse the validator using unobtrusive validation form.data('validator', null); $.validator.unobtrusive.parse(form); });
The above is the detailed content of How to Apply Class Validation to Dynamic Textboxes in a Table Using jQuery Unobtrusive Validation?. For more information, please follow other related articles on the PHP Chinese website!