Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating Nested Models
    text
    copied!<p>I currently have a ViewModel setup as such:</p> <pre><code>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; } } </code></pre> <p>...in this case, "RequiredIfOtherPropertyIsTrue" is a validator that does pretty much what it says: checks to see if another property is true (in this case, our boolean indicating whether or not a deposit is required), and BankInfo is another model that looks something like this:</p> <pre><code>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; } } </code></pre> <p>Now, in our ViewModel, we have a checkbox bound to our DepositRequired boolean, and an EditorFor w/ a custom template for BankInfo. Upon submission, we're having trouble figuring out how disable validation on BankInfo <em>IF</em> it's not required by the model (eg. even if we don't require the property on the ViewModel, it's still triggering val on BankInfo and therefore, failing miserably on any form post). Is there any standard way for dealing w/ nested model validation on ViewModel bind?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload