c# - Getting Checkbox Value in ASP.NET MVC Action -


i'm working on asp.net mvc app. model form looks following:

public class viewmodel {     public bool isactive { get; set; } } 

in form, have following html.

<label class="checkbox pull-left">    <input type="checkbox" id="isactive" name="isactive" data-val="true" data-val-required="the isactive field required.">    <i></i>active/deactive    <input name="isactive" type="hidden"> </label> 

when post form, isactive value in model false. however, name property in model has value. i've tested setting breakpoint in following:

[httppost] public actionresult myaction(viewmodel model) {     // model.isactive false } 

why isn't checkbox value getting set?

what should fix this?

you not setting value attribute in either checkbox or associated hidden input no value posted , value of property default value (false). needs be

<label class="checkbox pull-left">     <input type="checkbox" id="isactive" name="isactive" value="true" ....>     <i></i>active/deactive     <input name="isactive" type="hidden" value="false"> </label> 

side note: there reason you're not using @html.checkboxfor(m => m.isactive) generate correct html?


Comments