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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:55:37+00:00 2026-05-25T10:55:37+00:00

My problem is somewhat similar to this problem Custom Validation on group of checkboxes

  • 0

My problem is somewhat similar to this problem Custom Validation on group of checkboxes

I have a group of check boxes in the view (New.cshtml) which are rendered using ‘ParentObject’ as a view model

ParentObject.cs :-

public class ParentObject
{

  [Required]
  public IEnumerable<RegionObject> Regions { get; set; }

}

New.cshtml :-

@model ParentObject

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()){   
@Html.LabelFor(model => model.Regions)
@Html.EditorFor(model => model.Regions)
@Html.ValidationMessageFor(model => model.Regions)
<input type="submit" title="Create new parent object" class="button btn67" value="Create" />
}

The editor template for regions is in \Views\Shared\EditorTemplates\RegionObject.cshtml
which is like this

@model RegionObject
<p>
@Html.CheckBoxFor(m => m.IsChecked)
@Html.HiddenFor(m => m.Code)
@Html.DisplayFor(m => m.Name)
</p>

The RegionObject view model is :

public class RegionObject
{
   public Guid Code { get; set; }
   public string Name  { get; set; }
   public bool IsChecked { get; set; }
}

Why is the ‘Required’ data annotation on ‘Regions’ not working? I want to make sure at least 1 check box is selected against Regions. I would have thought ‘Required’ would do the job but apparently not. Do I need to write a custom validator to make this work?

Thanks.

  • 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-25T10:55:37+00:00Added an answer on May 25, 2026 at 10:55 am

    The Required attribute cannot operate on collections as you might wish. Also how do you expect it to know that at least one checkbox need to be selected from this RegionObject custom class? How do you expect it to relate the IsChecked property in this collection?

    Do I need to write a custom validator to make this work?

    Yes, you need a custom validation attribute to achieve that. For example:

    public class AtLeastOneRegionMustBeCheckedAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var instance = value as IEnumerable<RegionObject>;
            if (instance != null)
            {
                return instance.Where(x => x.IsChecked).Count() > 0;
            }
            return base.IsValid(value);
        }    
    }
    

    and then:

    public class ParentObject
    {
        [AtLeastOneRegionMustBeChecked(ErrorMessage = "Please select at least one region")]
        public IEnumerable<RegionObject> Regions { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am somewhat new to jQuery and I have a problem with something I
So, my question is somewhat similar to these two questions: Custom View onDraw is
This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements
This is a really strange problem, that appears to be somewhat intermittent (although it
Problem: I have an address field from an Access database which has been converted
I have a query that UNION 's two somewhat similar datasets, but they both
This question is somewhat similar to this one Best way to deploy large *.war
I have a somewhat grouping system which all the elements between two divs are
I'm wondering if this is something somewhat simple, but I'm having a problem ONLY
I'll rewrite a different question of mine, because the problem case somewhat changed: If

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.