0

I'm trying to bind some checkboxes to the model but I get null values.

So basically, I have designationList: possibilities that user can check or not. There is id name and description (name and description used for user in UI, and Id to know the id selected) Model should bind selected values to the "designation" field, that can save id's as List.

Here is how I made the selectBoxes:

@foreach (var item in Model.designationList)
                {
                    <label>
                        @Html.CheckBox("CheckBoxName", new { id = "designation" + item.id, value = item.id }) @item.name: <span style="font-weight:lighter">@item.description</span>
                    </label>
                    <br />
                }

Here is the part of the model that interest us:

        [Required]
        public List<IncidentDesignation> designationList { get; set; }
        [Required]
        public List<int> designation { get; set; }

And this is the subobject:

public class IncidentDesignation
    {
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
    }

And finally, this is the Html Generated:

<div class="col-md-10">
   <label>
   <input id="designation1" name="CheckBoxName" type="checkbox" value="1"><input name="CheckBoxName" type="hidden" value="false"> Acte dangereux: <span style="font-weight:lighter">action qui met en danger une ou plusieurs personnes</span>
   </label>
   <br>
   <label>
   <input id="designation2" name="CheckBoxName" type="checkbox" value="2"><input name="CheckBoxName" type="hidden" value="false"> Situation dangereuse: <span style="font-weight:lighter">situation qui tôt ou tard, conduit à blesser des personnes ou causer des dégâts matériels importants</span>
   </label>
   <br>
   <label>
   <input id="designation3" name="CheckBoxName" type="checkbox" value="3"><input name="CheckBoxName" type="hidden" value="false"> Presqu'accident: <span style="font-weight:lighter">évènement qui aurait pu causer un accident de travail avec ou sans dégâts matériels</span>
   </label>
   <br>
   <label>
   <input id="designation4" name="CheckBoxName" type="checkbox" value="4"><input name="CheckBoxName" type="hidden" value="false"> Accident: <span style="font-weight:lighter">événement avec dommages corporels</span>
   </label>
   <br>
   <label>
   <input id="designation5" name="CheckBoxName" type="checkbox" value="5"><input name="CheckBoxName" type="hidden" value="false"> Environnement: <span style="font-weight:lighter">situation qui risque d'engendrer un impact</span>
   </label>
   <br>
   <label>
   <input id="designation6" name="CheckBoxName" type="checkbox" value="6"><input name="CheckBoxName" type="hidden" value="false"> Incendie: <span style="font-weight:lighter">incendie ayant nécéssité une intervention afin de l'éteindre</span>
   </label>
   <br>
   <span class="text-danger field-validation-valid" data-valmsg-for="incidentDate" data-valmsg-replace="true"></span>
</div>

Thanks in advance to help me

edit:

Add Current Controller in test:

[HttpPost]
        public ActionResult Create(IncidentViewModel model)
        {
            if (ModelState.IsValid)
            { 
                // add record in db and retrieve IncidentId
                // Go to Add pictures with provided ID
            }
            return RedirectToAction("Create");
        }

I see null values when I breakpoint if statement.

Vočko
  • 2,478
  • 1
  • 23
  • 31
clement
  • 4,204
  • 10
  • 65
  • 133
  • 1
    Check the answer to this question: http://stackoverflow.com/questions/3234973/dynamic-list-of-checkboxes-and-model-binding – Dietz Jan 05 '15 at 09:13

1 Answers1

1

Use like below

@foreach (var item in Model.designationList)
{
    <label>
        <input type="checkbox" value="@item.id" name="CheckBoxName" id = "designation" + @item.id />@item.name: <span style="font-weight:lighter">@item.description</span>
    </label>
    <br />
}
Aravindan
  • 855
  • 6
  • 15
  • On which operation you post the check box value to controller. Please let me know the controller, and the view code the tag you used to through value – Aravindan Jan 05 '15 at 09:36
  • There are lots of check box with same name in side of the form. I think it may cause the problem. Kindly do some thing on that – Aravindan Jan 05 '15 at 09:55
  • work with @item.name Thanks! – clement Jan 05 '15 at 10:07