How to get materializecss checkbox to work with @Html.CheckBoxFor?

后端 未结 3 1647
醉酒成梦
醉酒成梦 2020-12-11 08:01

So I\'m having an issue trying to implement materializecss\' checkbox with @Html.CheckBoxFor. If I input exactly:



        
相关标签:
3条回答
  • 2020-12-11 08:36

    Try reversing your checkbox and label render order...

    @Html.CheckBoxFor(m => m.RememberMe, new { @type = "checkbox" })
    @Html.LabelFor(m => m.RememberMe, new { @class = "login-label" })
    
    0 讨论(0)
  • 2020-12-11 08:42

    I Was having same problem and the order of ChecBoxFor and LabelFor were like peter.edwards suggest. For me the problem was caused by a hidden element generated by @Html.CheckBoxFor:

    <input checked="checked" class="check-box" data-val="true" id="IsActive" name="IsActive" type="checkbox" value="true">
    <input name="IsActive" type="hidden" value="false">
    <label for="IsActive">IsActive</label>
    

    What I did to solve the problem was, move the hidden element to the bottom of the parent element:

     $("input[type='hidden']").each(function (index, element) { 
            $(this).appendTo($(element).parent());
        });
    
    0 讨论(0)
  • 2020-12-11 08:44
    Case 1:
    
    @Html.LableFor(x=>x.IsVpn)
    @Html.CheckBoxFor(x=>x.IsVpn) (Suppose)
    
    render like that
    <lable for="IsVpn">
    <input type="checkbox" name="IsVpn" value="true"/>
    <input type="hidden" name="IsVpn" value="false"/>
    
    
    But We Need like that For materialize css
    
    
    <input type="checkbox" name="IsVpn" value="true"/>
    <lable for="IsVpn">
    <input type="hidden" name="IsVpn" value="false"/>
    
    Case 2:
    
    @Html.CheckBoxFor(x=>x.IsVpn) (Suppose)
    @Html.LableFor(x=>x.IsVpn)
    
    Now What happen:
    
    <input type="checkbox" name="IsVpn" value="true"/>
    <input type="hidden" name="IsVpn" value="false"/>
    <lable name="IsVpn">
    
    But our requirement is for materilize css :
    
    <input type="checkbox" name="IsVpn" value="true"/>
    <lable name="IsVpn">
    <input type="hidden" name="IsVpn" value="false"/>
    
    So we can't use @Html.CheckBoxFor materializ css directly
    
    but it we can use output:
    
    <input type="checkbox" name="IsVpn" value="true"/>
    <lable name="IsVpn">
    <input type="hidden" name="IsVpn" value="false"/>
    
    it will work exactly same as checkboxfor.
    
    But If we want razor syntax then we need to customize checkboxfor and need to make a extension..
    
    0 讨论(0)
提交回复
热议问题