Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:44:39+00:00 2026-06-16T13:44:39+00:00

I currently have a ViewModel setup as such: public class OurViewModel { public OurViewModel()

  • 0

I currently have a ViewModel setup as such:

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 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:

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 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 IF 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:44:41+00:00Added an answer on June 16, 2026 at 1:44 pm

    Unfortunately, with the built in validation, you’d have to use

    ModelState.Remove("BankInfo");
    

    to conditionally ignore any validation failures on that object.

    If using FluentValidation is an option, you can do something like this in your OurViewModelValidator:

    RuleFor(ourViewModel=> ourViewModel.BankInfo).SetValidator(new BankInfoValidator()).When(ourViewModel=> ourViewModel.DepositRequired);
    

    and then let the BankInfoValidator handle validation of that object.

    Something like:

    public class BankInfoValidator : AbstractValidator<BankInfo>
    {
        public BankAccountInfoValidator() 
        {
            RuleFor(bank => bank.AccountName).NotEmpty().WithMessage("You must provide a name for your bank account.");
            RuleFor(bank => bank.AccountNumber).NotEmpty().WithMessage("You must provide an account number for your bank information.");
            RuleFor(bank => bank.AccountType).NotEmpty().WithMessage("You must select what kind of bank account you're entering information for (checking, savings).");
            RuleFor(bank => bank.ABANumber).NotEmpty().WithMessage("You must provide a routing number for your bank information.");
            RuleFor(bank => bank.ABANumber).Must(BeOnlyDigits).WithMessage("Your routing number can only contain numeric characters, 0-9.");
            RuleFor(bank => bank.AccountNumber).Must(BeOnlyDigits).WithMessage("Your account number can only contain numeric characters, 0-9.");
        }
    
        private bool BeOnlyDigits(string digitString)
        {
            int result;
            return int.TryParse(digitString, out result);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a ViewModel set up for a blog: public class PostViewModel {
I have an ASP.NET ViewModel like this: public class ParentViewModel { public ChildViewModel Child
I currently have a form with inputs and name attributes. I'm able to get
I have a viewmodel class for a create view that consists of 2 properties
I currently have a repository setup that loads forms a person would need to
I currently have an EF class that is backed by a database view (joining
Good evening everyone. I am currently using MVC 3 and I have a viewmodel
I have this decimal set up: [Required(ErrorMessage = Please tell us your income)] [Display(Name
Suppose I have a ViewModel with 100 props. Currently I need a one handler
Our current MVC project is set up to have ViewModels that encapsulate the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.