.net - how to bind checkbox dynamically in list type model mvc 5 -


i have class

public class custombenefitcommittee     {         public int planoptionid { get; set; }         public bool ischecked { get; set; }         public string planoptionname { get; set; }     } 

and have took class list type of model in razer view

@model list<mihr.business.customclasses.custombenefitcommittee> @{     int counter = 0; }  <table class="table table-bordered" id="tblplanlst">         <thead>             <tr>                 <th>                      select                 </th>                 <th>                     benefit option                 </th>              </tr>         </thead>          <tbody>              @foreach (var enrol in model)             {                 <tr>                     <td> <input type="checkbox" name="custombenefitcommittee[@counter].ischecked"  class="clscheck" /></td>                     <td>                         @enrol.planoptionname                     </td>                  </tr>                         counter++;             }          </tbody>     </table> <input type="submit" id="btnsubmitsave" value="save" name="buttontype" class="btn btn  btn-primary"> 

now when post form controller checking checkbox false in checkbox ischecked property. can me out it.

you can use way :

@{     var ischecked = "";     if (enrol.ischecked == true)     {         ischecked = "checked='checked'";     } } <input type="checkbox" name="custombenefitcommittee[@counter].ischecked" @ischecked value="true" /> 

in true if checkbox checked , null while checkbox unchecked

public class custombenefitcommittee     {         public int planoptionid { get; set; }         public bool? ischecked { get; set; }         public string planoptionname { get; set; }     } 

or can use way :

@html.checkbox("custombenefitcommittee[" + counter + "].ischecked", enrol.ischecked ?? false, new { @class = "clscheck" }) 

or can use way :

@html.checkboxfor(model => model[@counter].ischecked) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -