Is there a way to include @Html.ValidationMessageFor()
for each item
in a collection
? Let's take an example,
public class FooVm
{
// some property
public ICollection<BarVm> Bars { get; set; }
}
public class BarVm
{
// some property
[Range(1, int.Max, ErrorMessage = "Must be greater than 1")
public float? Fox { get; set; }
}
Now, when working with a view
,
@model namespace.here.FooVm
<div class="container"></div>
<a href="#" class="trigger">Populate</a>
<script>
$(function() {
var i = 0;
var populate = function() {
var strBuilder = '<input type="text" name="Bars[i].Fox" />';
$(".container").append(strBuilder);
return false;
};
$(".trigger").click(populate);
});
</script>
Everything seems to be functioning correctly. However, is there a way to add validation to each textbox
? I'm still learning ASP.NET MVC 4
and currently using unobtrusive validation
for client-side validation. Any recommendations or sample code on how to achieve this would be greatly appreciated. Thank you.