简体   繁体   中英

How to create a lists of checkBox in a View using asp.net mvc?

I have a View and calling my model for checkbox to have a lists to each other. Each checkbox to a line must have its own and using bootstrap. But they not what i expect them to do. Please assist and attached the mock-up below as what they should be. My logic to do this is as below.

// View
Dietary requirements
 @Html.CheckBoxFor(model => model.DietMain.None)<label for="None"> None</label> </div> <div class="row"> <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12"> @Html.CheckBoxFor(model => model.DietMain.Vegetarian)<label for="Vegetarian">Vegetarian</label> </div> </div> </div>

在此处输入图像描述

You can make it like this:

@{
    var checkboxList = new List<string> { "None", "Vegetarian" };
}

@foreach (var checkbox in checkboxList)
{
    <div>
        <div class="row">
            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                @Html.CheckBox(checkbox, new { Name = $"DietMain_{checkbox}"})
                <label for="@checkbox">@checkbox</label>
            </div>
        </div>

    </div>
}

Instead of CheckBoxFor, use CheckBox with setting Name attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM