Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Team

I cant seem to make my list of checkBoxFor correctly they seem screwed somehow and i am using boostrap on my Razor@html.CheckboxFor().. I want to create a list be aligned to a new line each time.

What I have tried:

C#
<pre>                        <div class="form-group row">
                            <label for="Dietary requirement" class="col-sm-2 col-form-label">Dietary requirements</label>
                            <div class="col-sm-2">
                                @Html.CheckBoxFor(model => model.DietMain.None)<label for="None">None</label>

                            </div>


                        </div>
                        <div class="col-sm-2">
                            @Html.CheckBoxFor(model => model.DietMain.Halaal)<label for="Halal">Halaal</label>
                        </div>

                    </div>
Posted
Updated 3-Jul-20 0:20am

1 solution

The Bootstrap grid layout has 12 columns:
Grid system · Bootstrap v4.5[^]

You have specified that you want the label to take up 2 columns, and each of the checkboxes to take up 2 columns. That means all three will fit on a single row, taking up half the width of the container.

You'll also want to use the correct Bootstrap markup for your checkboxes:
Forms · Bootstrap v4.5[^]

Try something like this:
Razor
<div class="form-group row">
    <label class="col-sm-2 col-form-label">Dietary requirements:</label>
    <div class="col-sm">
        <div class="form-check">
            @Html.CheckBoxFor(m => m.DietMain.None, new { @class = "form-check-input" })
            <label class="form-check-label" for="@Html.IdFor(m => m.DietMain.None)">None</label>
        </div>
        <div class="form-check">
            @Html.CheckBoxFor(m => m.DietMain.Halaal, new { @class = "form-check-input" })
            <label class="form-check-label" for="@Html.IdFor(m => m.DietMain.Halaal)">None</label>
        </div>
    </div>
</div>

NB: Based on the provided options, they appear to be exclusive - it wouldn't make sense to select both "none" and "halaal". If the user can only select one option, it would make more sense to use a radio-button list instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900