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

  • Home
  • SEARCH
  • 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 7130077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:18:41+00:00 2026-05-28T11:18:41+00:00

I am constructing a page using Editor Templates and composition. My view model contains

  • 0

I am constructing a page using Editor Templates and composition. My view model contains properties which are themselves view models. E.g.

public class ParentModel
{
    public boolean SomeCheckBox { get; set; }

    public ChildModel Child { get; set; }
}

public class ChildModel
{
    [Required]
    public string SomeString { get; set; }

    [Required]
    public string SomeOtherString { get; set; }
}

I would like the data annotation validation to kick in on the child only if the property SomeCheckBox on the parent is true.

I’ve seen a RequiredIf custom validation attribute elsewhere on stackoverflow, however it only works when the condition is a value of the same view model. I need something that can check the parent, or indeed a property on an ancestor.

My temporary hack is to clear the ModelState errors on postback if the checkbox isn’t true.

I’ve also had to write some custom javascript so that the client browser suppresses the validation if the checkbox isn’t ticked.

The real example is much more complicated than this but hopefully it’s clear from the above simplified example what I’m after.

What would be nice is an attribute on the parent view model that is something like

public class ParentModel
{
    public boolean SomeCheckBox { get; set; }

    [SuppressValidationIf("SomeCheckBox", false)]
    public ChildModel Child { get; set; }
}

Any ideas?

  • 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-05-28T11:18:42+00:00Added an answer on May 28, 2026 at 11:18 am

    This excellent example perfectly illustrates the limitations of doing declarative validation which is what data annotations are.

    It’s for this reason that I would recommend you using an imperative approach for your validation rules as you can handle many scenarios. FluentValidation.NET is a great example of a library which would have rendered this validation scenario a piece of cake.

    Let me illustrate how it could handle this scenario:

    We start by defining validators for our child and parent models:

    public class ChildModelValidator : AbstractValidator<ChildModel>
    {
        public ChildModelValidator()
        {
            RuleFor(x => x.SomeString).NotEmpty();
            RuleFor(x => x.SomeOtherString).NotEmpty();
        }
    }
    
    public class ParentModelValidator : AbstractValidator<ParentModel>
    {
        public ParentModelValidator()
        {
            RuleFor(x => x.Child)
                .SetValidator(new ChildModelValidator())
                .When(x => x.SomeCheckBox);
        }
    }
    

    Notice how the child validator is included based on the value of the SomeCheckBox property on the parent? Now your models would look like this:

    [Validator(typeof(ParentModelValidator))]
    public class ParentModel
    {
        public bool SomeCheckBox { get; set; }
        public ChildModel Child { get; set; }
    }
    
    public class ChildModel
    {
        public string SomeString { get; set; }
        public string SomeOtherString { get; set; }
    }
    

    And that’s all.

    You simply install the FluentValidation.MVC3 NuGet, add the following line in Application_Start:

    FluentValidationModelValidatorProvider.Configure();
    

    and now you work as usual:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new ParentModel
            {
                Child = new ChildModel()
            });
        }
    
        [HttpPost]
        public ActionResult Index(ParentModel model)
        {
            return View(model);
        }
    }
    

    and a view:

    @model ParentModel
    
    @Html.ValidationSummary(false)
    @using (Html.BeginForm())
    {
        @Html.CheckBoxFor(x => x.SomeCheckBox)
        @Html.EditorFor(x => x.Child.SomeString)
        @Html.EditorFor(x => x.Child.SomeOtherString)
        <button type="submit">OK</button>
    }
    

    If the checkbox is checked the child validator will kick in and require the 2 properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm constructing an ecommerce application in Symfony, and I have a page which lists
I am constructing a page which uses jQuery to assign event handlers. Now I
I am constructing a search page with a textbox and a button for now,
I need to call a few webservices while constructing an MVC page, so I'm
I'm constructing a program which uses mprotect() to restrict a block of memory from
I'm using a RegExp to validate some user input on an ASP.NET web page.
Using MVC2 Have a master-page that needs to hide certain menus if currently logged
Is there a tool similar to Eclipse Based Visual SCXML Editor for constructing Harel
I am currently constructing a wordpress website with a page that allows questions to
I've created a WCF Data Service with a base class of my EF model.

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.