0

I have a viewmodel with the following two fields:

public bool IsMobile {get; set;}
public bool? AcceptSms {get; set;}

If IsMobile is set to true, I need to require a value for AcceptSms, not null. I cant figure out the structure to use the get or set function to require value.

Something like below is what I think I need ...

public bool? AcceptSms 
{ get 
    { if(IsMobile == true)
       {return ...}
    }
}
Bart Daley
  • 70
  • 7
  • 1
    Is it not an option to default AcceptSms to false and make it not nullable? But have a look at conditional validation attributes to help with the front end :http://stackoverflow.com/questions/20642328/how-to-put-conditional-required-attribute-into-class-property-to-work-with-web-a and/or http://stackoverflow.com/questions/26898181/c-sharp-custom-attribute-required-if – Van Jan 08 '15 at 06:23
  • If you want both server side and client side validation, [foolproof](http://foolproof.codeplex.com/) have some useful conditional validation attributes including `[RequiredIfTrue]` which should suit your needs. –  Jan 09 '15 at 05:59
  • Thanks! Exactly what I was looking for. If you'll add as answer, I'll accept. – Bart Daley Jan 09 '15 at 08:01

0 Answers0