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 3623570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:23:59+00:00 2026-05-18T23:23:59+00:00

I have an ASP.NET MVC 2 project in which I’ve created a data transfer

  • 0

I have an ASP.NET MVC 2 project in which I’ve created a data transfer object to receive data from a web page form. The form has two groups of checkboxes on it. I want to validate the object to make sure that at least one of the checkboxes in each group is checked.

I’m doing the validation on the server side so that a user won’t be able to hack around any client-side validation. (I will add client-side validation with jQuery later; that’s easy.)

My understanding is that I have to create my own custom ValidationAttribute for my data transfer object class, but I don’t understand how to create and use one that can accept an arbitrary list of checkbox properties to make sure that at least one of them is true. I am guessing I will have to call the attributes like this:

[AtLeastOneCheckbox("set1check1", "set1check2", "set1check3",
    ErrorMessage = "You must check at least one checkbox in set 1.")]
[AtLeastOneCheckbox("set2check1", "set2check2", "set2check3", "set2check4", "set2check5",
    ErrorMessage = "You must check at least one checkbox in set 2.")]
public class MyFormDTO
{
    ...
}

What would the implementation of AtLeastOneCheckboxAttribute look like?

Or is there a different way that I should do this kind of validation?

  • 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-18T23:24:00+00:00Added an answer on May 18, 2026 at 11:24 pm

    if you have several checkbox groups, you just need to deine the attribute several times.

    [AttributeUsage( AttributeTargets.Class)]
    public class AtLeastOneCheckboxAttribute : ValidationAttribute
    {
        private string[] _checkboxNames;
    
        public AtLeastOneCheckboxAttribute(params string[] checkboxNames)
        {
            _checkboxNames = checkboxNames;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var propertyInfos = value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(x=>_checkboxNames.Contains(x.Name));
    
            var values = propertyInfos.Select(x => x.GetGetMethod().Invoke(value, null));
            if (values.Any(x => Convert.ToBoolean(x)))
                return ValidationResult.Success;
            else
            {
                ErrorMessage = "At least one checkbox must be selected";
                return new ValidationResult(ErrorMessage);
            }
        }
    }
    

    UPDATE

    as you have found out, class-level validation is called only after all properties pass. In order to get the error, just use empty string as the key.

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

Sidebar

Related Questions

I'm working on a Web project using Asp.Net MVC, which I'll have to deploy
Currently I have an Area in my ASP.NET MVC 2 project call 'API', which
I'm working on a few ASP.NET MVC projects which all require database functionality. Unfortunately,
I am trying to create a pluggable ASP.NET MVC framework. I have extensively used
I want to structure my ASP.NET MVC 2 web application sensibly using Areas. The
I have written a VERY simple MVC application which just displays a single string
What are the limitations, unexpected pitfalls and performance traits of moving from in process
I'm trying to use WCF Data Service with Subsonic, but ran into this error
I'm having trouble right now with the creation of a Master-Detail page in an
I would like to get opinion on the best way to manage the following

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.