I have a View as Follows
@model List<item>
@using (Html.BeginForm("Create", "Item", FormMethod.Post))
{
@for (int i = 0; i < Model.Count; i++)
{
.....
@Html.EditorFor(model => Model[i].ItemName)
.....
}
<input type="submit" class="btn btn-primary" value="Create Item" />
}
In the ViewModel this ItemName has a Required attribute annotation for Validation purposes, but what I really need is at least one ItemName to be filled to assume that this Model is valid, but I will always get The ModelState IsValid = False
I was able to solve this by using:
public class CreateItemCustomValidation : ValidationAttribute
{
public override bool IsValid(object value)
{
var list = value as List<SingleItem>;
if (list != null)
{
if (list.Where(o => o.ItemName!= null && !String.IsNullOrEmpty(o.ItemName) && !String.IsNullOrWhiteSpace(o.ItemName)).Count() > 0)
{
return true;
}
}
return false;
}
}
The validation works, but the validation is firing before POST