Asp.net 如何为引导复选框创建EditorTemplate?
我正在使用引导模板,其复选框模板如下所示:Asp.net 如何为引导复选框创建EditorTemplate?,asp.net,asp.net-mvc,twitter-bootstrap,checkbox,mvc-editor-templates,Asp.net,Asp.net Mvc,Twitter Bootstrap,Checkbox,Mvc Editor Templates,我正在使用引导模板,其复选框模板如下所示: <div class="checkbox"> <label> <input type="checkbox" class="checkbox style-1" checked="checked"> <span>Checkbox 1</span> </label> </div> CheckBoxFor()方法生成2个输入以确
<div class="checkbox">
<label>
<input type="checkbox" class="checkbox style-1" checked="checked">
<span>Checkbox 1</span>
</label>
</div>
CheckBoxFor()
方法生成2个输入以确保值被发回(未选中的复选框不提交值,因此隐藏的输入确保提交false
),并且您不应尝试更改此行为。您尝试的EditorTempate
无法工作,原因有很多,包括复选框(有2个状态)无法绑定到nullable bool
(有3个状态)和else
块意味着始终提交false
值,即使选中复选框
相反,请使用CheckBoxFor()
方法,但要调整css选择器
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.IsActive, new { @class = "checkbox style-1" })
<span>Checkbox 1</span>
</label>
</div>
它获取放置在需要更改为的复选框之后的span
元素
label input[type=checkbox].checkbox ~ span:before {
其他选择器也是如此(即将+
更改为~
)。~
选择器匹配第二个元素,如果它前面有第一个元素,并且两个元素共享一个共同的父元素(请参阅)我认为您需要的只是一个让您认为需要更改它的元素。您可以使用CheckBoxFor()
方法添加class=“checkbox style-1”
。但是很明显,您不理解CheckBoxFor()
方法。首先,它生成隐藏的输入,以便始终回发值(未选中的复选框不提交值)。其次,您不能将复选框绑定到可为空的bool
-复选框有2个状态,但bool?
有3个状态,而您的EditorTemplate
没有任何意义-您的else
块生成一个复选框,无论其是否选中,该复选框都将始终提交一个值false
。@StephenMuecke:您知道吗意思是:@Html.CheckBoxFor(m=>m.EmaiToCustomer,new{@class=“checkbox style-1”})@Html.LabelFor(m=>m.EmaiToCustomer)
如果是,当隐藏输入出现在复选框输入之后时,会导致复选框图标不再可见!那么我猜你是在使用插件。您需要显示相关的代码,包括用于“图标”的css!(这只是更改css
选择器的问题,目前最好的猜测是.checkbox+label
)@Manoz您认为在控制器中获取值有什么不同吗。问题是需要额外的隐藏输入才能在控制器中获取值,并且必须在span之前。
label input[type=checkbox].checkbox + span:before {
font-family: FontAwesome;
font-size: 12px;
border-radius: 0;
content: " ";
display: inline-block;
text-align: center;
vertical-align: middle;
padding: 1px;
height: 12px;
line-height: 12px;
min-width: 12px;
margin-right: 5px;
border: 1px solid #bfbfbf;
background-color: #f4f4f4;
font-weight: 400;
margin-top: -1px;
}
label input[type=checkbox].checkbox + span:before {
content: " ";
}
label input[type=checkbox].checkbox:checked + span:before {
content: "";
color: #2e7bcc;
}
label input[type=checkbox].checkbox:checked + span {
font-weight: 700;
}
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.IsActive, new { @class = "checkbox style-1" })
<span>Checkbox 1</span>
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="IsActive" class="checkbox style-1" ... value="true">
<input type="hidden" name="IsActive" value="false">
<span>Checkbox 1</span>
</label>
</div>
label input[type=checkbox].checkbox + span:before {
label input[type=checkbox].checkbox ~ span:before {