ASP.Net MVC: GetClientValidationRules() not working with list type property
<p>still got no solution for my situation. i want to validate <code>checkboxes</code> by <code>ValidationAttribute, IClientValidatable.</code></p>

<p>my <code>GetClientValidationRules()</code> function is not getting called because i impose my custom attribute on a list type property in view model.</p>

<pre><code>[AtleastOne(ErrorMessage = "Select at least one checkbox.")]
public List&lt;Hobby&gt; Hobbies { get; set; }
</code></pre>

<p>if i impose <strong>AtleastOne attrbute</strong> on a property which does not return list then <strong>GetClientValidationRules() function is calling</strong>.</p>

<p>anyone can tell me what is the reason for which <code>GetClientValidationRules()</code> function is not calling when working with list type property.</p>

<p>please some one help me with right direction. thanks</p>

<h2>EDIT</h2>

<h2>see my model and view model code</h2>

<pre><code>public class Product
{
    public int ID { set; get; }
    public string Name { set; get; }
}

public class Hobby 
{
    public string Name { get; set; }
    public bool IsSelected { get; set; }

}

public class SampleViewModel
{
    [Display(Name = "Products")]
    public List&lt;Product&gt; Products { set; get; }

    [AtleastOne(ErrorMessage = "Select at least one checkbox.")]
    public List&lt;Hobby&gt; Hobbies { get; set; }

    [Required(ErrorMessage = "Select any Product")]
    public int SelectedProductId { set; get; }

    [Required(ErrorMessage = "Select Male or Female")]
    public string Gender { get; set; }
}
</code></pre>

<h2>custom validation code</h2>

<pre><code>[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class AtleastOneAttribute : ValidationAttribute, IClientValidatable
{
    // For Server side
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value != null)
        {
            var oHobby=value as IEnumerable;

            foreach (var _object in oHobby)
            {
                Hobby _oHobby = (Hobby)_object;
                if (_oHobby.IsSelected)
                {
                    return ValidationResult.Success;
                }
            }

        }
        return new ValidationResult(ErrorMessage);
    }
    // Implement IClientValidatable for client side Validation
    public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "atleastonetrue",
        };
        yield return rule;
    }
}
</code></pre>

<h2>See how i am generating checkboxes in loop</h2>

<pre><code>    &lt;div class="col-md-offset-2 col-md-10"&gt;
        &lt;b&gt;Hobbies&lt;/b&gt;&lt;br /&gt;
        @for (int x = 0; x &lt; Model.Hobbies.Count(); x++)
        {
            @Html.CheckBoxFor(p =&gt; p.Hobbies[x].IsSelected, new { @class = "hobbycls" }) @: &amp;nbsp; 
            @Html.LabelFor(p =&gt; p.Hobbies[x].IsSelected, Model.Hobbies[x].Name) @: &amp;nbsp;
            @Html.HiddenFor(p =&gt; p.Hobbies[x].Name) 
        }
        @Html.ValidationMessageFor(model =&gt; model.Hobbies)
    &lt;/div&gt;
</code></pre>

<h2>client side code</h2>

<pre><code>$(function () {
    $.validator.unobtrusive.adapters.add('atleastonetrue', [], function (options) {
        options.rules['restrictbackdates'] = { mindate: options.params.mindate };
        options.messages['atleastonetrue'] = options.message;
        options.rules['atleastonetrue'] = options.params;
        alert('hello');
        alert(options.message);
    });

    $.validator.addMethod("atleastonetrue", function (value, element, param) {
        return $('.hobbycls:checked').size() &gt; 0;
    });
});
</code></pre>

<h2>see the html for checkboxes</h2>

<pre><code>&lt;div class="col-md-offset-2 col-md-10"&gt;
&lt;b&gt;Hobbies&lt;/b&gt;&lt;br&gt;
&lt;input type="checkbox" value="true" name="Hobbies[0].IsSelected" id="Hobbies_0__IsSelected" data-val-required="Please check at least one box" data-val="true" class="hobbycls input-validation-error"&gt;&lt;input type="hidden" value="false" name="Hobbies[0].IsSelected"&gt;  &amp;nbsp; 
&lt;label for="Hobbies_0__IsSelected"&gt;Reading&lt;/label&gt;  &amp;nbsp;
&lt;input type="hidden" value="Reading" name="Hobbies[0].Name" id="Hobbies_0__Name"&gt;&lt;input type="checkbox" value="true" name="Hobbies[1].IsSelected" id="Hobbies_1__IsSelected" data-val-required="Please check at least one box" data-val="true" class="hobbycls input-validation-error"&gt;&lt;input type="hidden" value="false" name="Hobbies[1].IsSelected"&gt;  &amp;nbsp; 
&lt;label for="Hobbies_1__IsSelected"&gt;Sports&lt;/label&gt;  &amp;nbsp;
&lt;input type="hidden" value="Sports" name="Hobbies[1].Name" id="Hobbies_1__Name"&gt;&lt;input type="checkbox" value="true" name="Hobbies[2].IsSelected" id="Hobbies_2__IsSelected" data-val-required="Please check at least one box" data-val="true" class="hobbycls input-validation-error"&gt;&lt;input type="hidden" value="false" name="Hobbies[2].IsSelected"&gt;  &amp;nbsp; 
&lt;label for="Hobbies_2__IsSelected"&gt;Movies&lt;/label&gt;  &amp;nbsp;
&lt;input type="hidden" value="Movies" name="Hobbies[2].Name" id="Hobbies_2__Name"&gt;                &lt;span data-valmsg-replace="true" data-valmsg-for="Hobbies" class="field-validation-valid"&gt;&lt;/span&gt;
&lt;/div&gt;    
</code></pre>

<p>1) the main problem is my client side js code is not firing
2) when i debug then notice this function <strong>GetClientValidationRules</strong> is not getting called because i use custom attribute on a property which return list type. here it is.</p>

<pre><code>[AtleastOne(ErrorMessage = "Select at least one checkbox.")]
public List&lt;Hobby&gt; Hobbies { get; set; }
</code></pre>

<p>so please see my code and tell me what is wrong in it ? thanks</p>