Model as below:
public class User
{
[DisplayName("ID")]
[Range(0, 9999)]
public int ID { get; set; }
public string Mask { get; set; }
}
And then controller returns IQueryable of above model
There are two views. Parent view and then partial view which renders each row from the Model
Main View
@model IQueryable<User>
@{
var Array = Model.ToArray();
}
//Header here
<tbody id="table-body-vlan">
@{
for (var i = 0; i < Array.Length; i++)
{
Html.RenderPartial("_AddRow", Array [i], new ViewDataDictionary() { { "Index", i } });
}
}
</tbody>
</table>
</div>
}
**And then partial view **
@model User
@{
var index = ViewData["Index "];
}
<tr class="js-deletable-item" id="row-@index" data-id="@Model.ID" >
<td class="editable">
@Html.TextBoxFor(v => Model.ID, new {Name = "ID[" + index + "]", id = "id-" + Model.ID, Value = Model. ID > 0 ? Model. ID.ToString() : ""})
</td>
<td class="editable">
@Html.TextBoxFor(v => Model.Name, new {Name = "Id[" + index + "].Name", data_val = "false" })
</td>
</tr>
Issue as below: If there are 10 records in the IQueryable then client side validations applies only to first row.
e.g. if I type 101S for ID field in first row I gets the warning (By changing the background color) that invalid ID. But if I do same for second or other rows then I don't see background color change. Client side validations (defined through Data Annotations) are applying only for first row.
I guess after first row rendered it is not adding those Annotation related properties to the other rows.