1

I have 2 ViewModels:

public class AViewModel
{
   public string Text {get; set;}
   //some other properties
}

And:

public class BViewModel
{
   public List<AViewModel> List {get; set;}
   //some other properties
}

I'm passing BViewModel to View and by default displaying 4 inputs for my list. I would like to achieve that validation is successful only if 2 or more inputs are non-empty, so my List in viewmodel must contain 2 AViewModel objects which have non-empty Text property. How can I achieve that? Should I try to write custom validation attribute or is there any other way?

monoh_
  • 1,019
  • 3
  • 13
  • 15
  • 1
    Are you trying to do server side only? client-side only? or both? if you are looking at doing both, you are best off trying to write your own. http://stackoverflow.com/questions/5146732/viewmodel-validation-for-a-list, here's a good place to start. – Jamie R Rytlewski May 10 '16 at 18:43
  • A custom validation attribute would be best, but you can always just check the collection in the POST method and add a `ModelStateError` is not valid and return the view. Either way you will not get client side vaidation. For that you will need to write your own client side scripts to display an error message. –  May 10 '16 at 22:57

0 Answers0