I currently have a ViewModel setting:
public class OurViewModel { public OurViewModel() { } [Required] public int LeadID { get; set; } [Required] public int Rate { get; set; } [Required] public bool DepositRequired { get; set; } [RequiredIfOtherPropertyIsTrue("DepositRequired")] public BankInfo { get; set; } }
... in this case, “RequiredIfOtherPropertyIsTrue” is a validator that does pretty much what it says: checks if another property is true (in this case our boolean value indicates whether a deposit is required) and BankInfo is another model which looks something like this:
public class BankInfo { public enum AccountTypeEnum { CHECKING, SAVINGS } public BankAccountInfo() { } [DisplayName("Account Number")] [Required(ErrorMessage = "You must provide a valid bank account number")] public String AccountNumber { get; set; } [DisplayName("Bank Routing Number")] [Required(ErrorMessage = "You must provide a valid routing number")] [StringLength(9, MinimumLength = 9, ErrorMessage = "Your bank routing number must be exactly 9 digits")] public String ABANumber { get; set; } [DisplayName("Bank Account Type")] [Required] public AccountTypeEnum AccountType { get; set; } [DisplayName("Name on Bank Account")] [Required(ErrorMessage = "You must provide the name on your bank account")] public String AccountName { get; set; } }
Now, in our ViewModel, we have a flag associated with our Boolean value DepositRequired, and an editor for a special template for BankInfo. After the submission, we are unable to figure out how to disable validation in BankInfo if it is not required by the model (for example, even if we do not require a property in ViewModel, it still runs val on BankInfo and therefore fails on any form message). Is there a standard way to check w / nested model when binding ViewModel?
source share