[RESOLVED]Custom Check box creation and Validation.


I am new to MVC. i have created custom Checkbox list control.

am able to bind the data to that control.

but Validation is not working when click on submit button.

Validation need to work like, i should select atleast one check box.

My Custom Class

 public static MvcHtmlString CheckboxForSelectList<TModel, TProperty>(
         this HtmlHelper<TModel> htmlHelper,
         Expression<Func<TModel, TProperty>> expression,
         IEnumerable<SelectListItem> listOfValues, string name)
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var sb = new StringBuilder();

            if (listOfValues != null)
                // Create a radio button for each item in the list 
                foreach (SelectListItem item in listOfValues)
                    // Generate an id to be given to the radio button field 
                    var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

                    // Create and populate a radio button using the existing html helpers 
                    var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
                    TagBuilder textBox = new TagBuilder("input");
                    textBox.Attributes.Add("type", "checkbox");
                    textBox.Attributes.Add("name", name);
                    textBox.Attributes.Add("id", "chk" + id);

                    textBox.Attributes.Add("aria-required", "true");
                    textBox.Attributes.Add("data-val", "true");
                    textBox.Attributes.Add("data-val-required", "This field is required.");
                    var chk = textBox.ToString(TagRenderMode.Normal);
                   // var chk = htmlHelper.CheckBoxFor(expression, new { id = id }).ToHtmlString();
                    // Create the html string that will be returned to the client 
                    // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label> 
                    sb.AppendFormat("<div class="CheckBox">{0}{1}</div>", chk, label);

            return MvcHtmlString.Create(sb.ToString());

UI rendered code:

<div class="CheckBox">
<input aria-required="true" data-val="true" data-val-required="This field is required." id="chkName_0" name="[4].Name" type="checkbox"></input>
<label id='Name_0'>Item 0</label>

<div class="CheckBox">
<input aria-required="true" data-val="true" data-val-required="This field is required." id="chkName_1" name="[4].Name" type="checkbox"></input>
<label id='Name_1'>Item 1</label>

<div class="CheckBox">
<input aria-required="true" data-val="true" data-val-required="This field is required." id="chkName_2" name="[4].Name" type="checkbox"></input>
<label id='Name_2'>Item 2</label>

<span class="field-validation-valid" data-valmsg-for="[4].Name" data-valmsg-replace="true"></span>

Please help me on this.



Hi Sundar1987,

Thanks for your post.


Validation need to work like, i should select atleast one check box.

You should custom validation attribute,like this according to your requirement to modify this and use it:

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)

        var values = propertyInfos.Select(x => x.GetGetMethod().Invoke(value, null));
        if (values.Any(x => Convert.ToBoolean(x)))
            return ValidationResult.Success;
            ErrorMessage = "At least one checkbox must be selected";
            return new ValidationResult(ErrorMessage);

Or you can try to use Jquery to implement it,please check this:

unobtrusive MVC3 validating group of checkboxes

Hope this can  be helpful.

Best Regards,


Leave a Reply