2017-08-16 74 views
0

我有一个名为PermanentAddressChk的字段保存在数据库中。显示复选框是和复选框否

我需要为PermanentAddressChk字段显示2个复选框'是'和'否'。

以下代码工作正常,但有没有更好的方法来做到这一点?打开新的想法,欢呼。

<br />@Html.Label("Yes") 
@Html.CheckBoxFor(m => m.PermanentAddressChk, htmlAttributes: new { @checked = true }) 
<br />&nbsp @Html.Label("No") 
@Html.CheckBoxFor(m => m.PermanentAddressChk, htmlAttributes: new { @checked = false}) 
+0

所以他们都可以检查?单选按钮用于多选选项,其中只能选择一个选项。 – Fran

回答

2

我知道你说过你在寻找两个复选框,但这里有一个例子,如果有任何兴趣的话,是/否单选按钮。

<div id="radioButtonDiv" > 
    @Html.LabelFor(model => model.PermanentAddressChk, "Is address permanent?") 
    <div> 
     @Html.RadioButtonFor(model => model.PermanentAddressChk, true, new { id = "isPermanentAddressTrue" }) @Html.Label("isPermanentAddressTrue", "Yes") 
     @Html.RadioButtonFor(model => model.PermanentAddressChk, false, new { id = "isPermanentAddressFalse" }) @Html.Label("isPermanentAddressFalse", "No") 
    </div> 
</div> 

这允许您通过单个PermanentAddressChk属性以及单个单选按钮值通过javascript引用控制器的bool值。这也会在单选按钮选项旁边显示“是/否”。

0

你可以这样做:

查看=>你只需要一个复选框,因为如果不检查,你会在控制器接收错误的。在视图模型

<div class="form-group"> 
    @Html.LabelFor(m => m.PermanentAddressChk, new { @class = "control-label col-md-2" }) 
    <div class="col-md-10"> 
     @Html.EditorFor(m => m.PermanentAddressChk) 
     @Html.ValidationMessageFor(m => m.Active) 
    </div> 
</div> 

插入此:

[Display(Name = "Permanent Address ?")] // This will appear in your label 
public bool PermanentAddressChk { get; set; } 

的CSS =>

.form-group { 
    margin-bottom: 15px; 
} 
.control-label { 
padding-top: 7px; 
margin-bottom: 0; 
text-align: right; 
margin-bottom: 0; 
vertical-align: middle; 
} 
+0

谢谢你们,非常感谢。 – Tom