How to get checkbox value from FormCollection?

后端 未结 5 474
耶瑟儿~
耶瑟儿~ 2021-01-02 03:18

I have a checkbox and button:

@using(Html.BeginForm())
{
    
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 03:46

    Include completed
    

    Will Post a value of "on" or "off".

    This WONT bind to a boolean,

    To receive a boolean value you can use HTML Helper Checkbox Like

    @Html.CheckBox("showAll")
    
    

    Update:

    If checked the html helper will post back "true,false" and if unchecked as "false"

    You can have this workaround as

    bool MyBoolValue= Convert.ToBoolean(collection["showAll"].Split(',')[0]);
    

    This is because the Html helpers class adds an extra hidden field to save the state of the checkbox

提交回复
热议问题