Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何为引导复选框创建EditorTemplate?_Asp.net_Asp.net Mvc_Twitter Bootstrap_Checkbox_Mvc Editor Templates - Fatal编程技术网

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 {