Send multiple checkbox via model to Controller

Asked

Viewed 37 times

1

I have a class:

public class TreeViewConfigVm
{
    public string plant_selected { get; set; }
    public List<SelectListItem> criticality { get; set; }
    public List<SelectListItem> actuator_construction { get; set; }
    public List<SelectListItem> positioner_type { get; set; }
    public List<SelectListItem> valve_type { get; set; }
    public List<SelectListItem> valve_size { get; set; }
    public List<SelectListItem> valve_end_connection { get; set; }
    public List<SelectListItem> area { get; set; }
}

For each property with List I have several item "Selectlistitem" that I have to POST from a form in Razor I am already "PICKING" the values: of each of the items as well as Selected, Value, Text but the Selected option which is a boleano always comes false to me when posting the form. Follow the example from VIEW:

<div class="col-sm-4 col-md-4 col-lg-4 no-padding">
                        <div class="card" style="height:170px;">
                            <div class="card-body">
                                <h5>Actuator Construction</h5>
                                <div id="values_actconstruction" style="height: 85px; overflow-y: auto; overflow-x: hidden;">
                                    @for (int i = 0; i < Model.actuator_construction.Count; i++)
                                    {
                                        @Html.CheckBoxFor(model => model.actuator_construction[i].Selected, new { value = Model.actuator_construction[i].Value }) <span>@Model.actuator_construction[i].Text</span> <br />
                                        @Html.HiddenFor(m => m.actuator_construction[i].Text)
                                        @Html.HiddenFor(m => m.actuator_construction[i].Value)
                                    }
                                </div>
                                <input id="command_actconstruction" type="hidden" />
                                <input id="checked_actconstruction" type="hidden" value="false" />
                                <button id="checkAllActuatorConstruction" type="button" class="btn btn-secondary btn-sm" style="position: absolute; top: 12px; right: 10px;"><i class="far fa-check-square"></i></button>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-4 col-md-4 col-lg-4">
                        <div class="card" style="height:170px;">
                            <div class="card-body">
                                <h5>Positioner Type</h5>
                                <div id="values_positionertype" style="height: 85px; overflow-y: auto; overflow-x: hidden;">
                                    @for (int i = 0; i < Model.positioner_type.Count; i++)
                                    {
                                        @Html.CheckBoxFor(model => model.positioner_type[i].Selected, new { value = Model.positioner_type[i].Value }) <span>@Model.positioner_type[i].Text</span> <br />
                                        @Html.HiddenFor(m => m.positioner_type[i].Text)
                                        @Html.HiddenFor(m => m.positioner_type[i].Value)
                                    }
                                </div>
                                <input id="command_positionertype" type="hidden" />
                                <input id="checked_positionertype" type="hidden" value="false" />
                                <button id="checkAllPositionerType" type="button" class="btn btn-secondary btn-sm" style="position: absolute; top: 12px; right: 10px;"><i class="far fa-check-square"></i></button>
                            </div>
                        </div>
                    </div>

                </div>

                <div class="row" style="margin-top:15px;">

                    <div class="col-sm-4 col-md-4 col-lg-4">
                        <div class="card" style="height:170px;">
                            <div class="card-body">
                                <h5>Valve Type</h5>
                                <div id="values_valvetypes" style="height: 85px; overflow-y: auto; overflow-x: hidden;">
                                    @for (int i = 0; i < Model.valve_type.Count; i++)
                                    {
                                        @Html.CheckBoxFor(model => model.valve_type[i].Selected, new { value = Model.valve_type[i].Value }) <span>@Model.valve_type[i].Text</span> <br />
                                        @Html.HiddenFor(m => m.valve_type[i].Text)
                                        @Html.HiddenFor(m => m.valve_type[i].Value)
                                    }
                                </div>
                                <input id="command_valvetypes" type="hidden" />
                                <input id="checked_valvetypes" type="hidden" value="false" />
                                <button id="checkAllValveType" type="button" class="btn btn-secondary btn-sm" style="position: absolute; top: 12px; right: 10px;"><i class="far fa-check-square"></i></button>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-4 col-md-4 col-lg-4 no-padding">
                        <div class="card" style="height:170px;">
                            <div class="card-body">
                                <h5>Valve Size</h5>
                                <div id="values_valvesize" style="height: 85px; overflow-y: auto; overflow-x: hidden;">
                                    @for (int i = 0; i < Model.valve_size.Count; i++)
                                    {
                                        @Html.CheckBoxFor(model => model.valve_size[i].Selected, new { value = Model.valve_size[i].Value }) <span>@Model.valve_size[i].Text</span> <br />
                                        @Html.HiddenFor(m => m.valve_size[i].Text)
                                        @Html.HiddenFor(m => m.valve_size[i].Value)
                                    }
                                </div>
                                <input id="command_valvesize" type="hidden" />
                                <input id="checked_valvesize" type="hidden" value="false" />
                                <button id="checkAllValveSize" type="button" class="btn btn-secondary btn-sm" style="position: absolute; top: 12px; right: 10px;"><i class="far fa-check-square"></i></button>
                            </div>
                        </div>
                    </div>

I have also linked the scripts below:

I don’t know if you need it but the fact is that your Setto one or more values of a checkbox group and everything comes false to me in my controller

My form template and stuff:

[X] bla bla bla 
[X] bla bla bla 
[ ] bla bla bla 
[ ] bla bla bla 

[ ] bla bla bla 
[ ] bla bla bla 
[X] bla bla bla 
[ ] bla bla bla 

[X] bla bla bla 
[ ] bla bla bla 
[ ] bla bla bla 
[X] bla bla bla 

What remains to be done ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.