0

How can I enforce to select only one option of the checkboxes being equal to "true" of a group? It can be with jQuery or Data Annotations...

Because currently you can submit with all being false, and I don't want to allow that, I want that only one of them must and need to be true, otherwise the submit won't work.

<div class="row">
    @Html.LabelFor(model => model.Question1CB1, htmlAttributes: new { @class = "control-label col-sm-2" })
    <div class="col-sm-1">
        <div class="checkbox">
            @Html.CheckBoxFor(model => model.Question1CB1, new { @class = "Question1CBs" })
            @Html.ValidationMessageFor(model => model.Question1CB1, "", new { @class = "text-danger" })
        </div>
    </div>

    @Html.LabelFor(model => model.Question1CB2, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-sm-1">
        <div class="checkbox">
            @Html.CheckBoxFor(model => model.Question1CB2, new { @class = "Question1CBs" })
            @Html.ValidationMessageFor(model => model.Question1CB2, "", new { @class = "text-danger" })
        </div>
    </div>

    @Html.LabelFor(model => model.Question1CB3, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-sm-1">
        <div class="checkbox">
            @Html.CheckBoxFor(model => model.Question1CB3, new { @class = "Question1CBs" })
            @Html.ValidationMessageFor(model => model.Question1CB3, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

The checkboxes already work that it can only be selected one of them.

<script>
    $(document).ready(function () {
        $('.Question1CBs').click(function () {
            $('.Question1CBs').not(this).prop('checked', false);
        });
    });
</script>
aesax
  • 35
  • 9

1 Answers1

0

There are several ways to solve your problem. but the standard way without using javascript is to use radio buttons. But you have to simulate your demand to set the proper boolean type to true. So change your view like this :

<div class="row">
@Html.LabelFor(model => model.Question1CB1, htmlAttributes: new { @class = "control-label col-sm-2" })
<div class="col-sm-1">
    <div class="checkbox">
        @Html.RadioButtonFor(model => model.SelectedQuestion , "CB1")
        @Html.ValidationMessageFor(model => model.Question1CB1, "", new { @class = "text-danger" })
    </div>
</div>

@Html.LabelFor(model => model.Question1CB2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-1">
    <div class="checkbox">
        @Html.RadioButtonFor(model => model.SelectedQuestion , "CB2")
        @Html.ValidationMessageFor(model => model.Question1CB2, "", new { @class = "text-danger" })
    </div>
</div>

@Html.LabelFor(model => model.Question1CB3, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-sm-1">
    <div class="checkbox">
        @Html.RadioButtonFor(model => model.SelectedQuestion, "CB3")
        @Html.ValidationMessageFor(model => model.Question1CB3, "", new { @class = "text-danger" })
    </div>
</div>

And in your action you should set your boolean property with checking the selected one :

[HttpPost]
    public JsonResult PostData(YourVMClass vm)
    {
        switch (vm.SelectedQuestion)
        {
            case "CB1":
                //Question1CB1 = true;
            case "CB2":
                //Question1CB2 = true;
            case "CB3":
                //Question1CB3 = true;
            default:
                break;
        }
    }
Amir Jelo
  • 347
  • 1
  • 7