2017-08-16 75 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