2012-05-29 92 views
1

我有我的asp.net的MVC編輯觀點裏面folloiwng單選按鈕選擇一個Html.Radiobutton: -我怎麼能根據對象的值

<div class="editor-label"> 
    @Html.LabelFor(model => model.InsuranceSettled) 
</div> 

<div class="editor-field"> 
    @Html.RadioButton("InsuranceSettled", 1) Yes 
    @Html.ValidationMessageFor(model => model.InsuranceSettled) 
</div> 

<div class="editor-field"> 
    @Html.RadioButton("InsuranceSettled", 0) No 
    @Html.ValidationMessageFor(model => model.InsuranceSettled) 
</div> 

但如何我可以選擇所需的單選按鈕accoring到模型值,所以如果model.InsuranceSettled值爲0,則選擇否單選按鈕,反之亦然.. BR

回答

3

我會建議使用01改爲。我認爲最好使用強類型助手來生成輸入,以保證輸入具有正確的名稱。他們也會根據您的模型自動選擇默認值。

@Html.RadioButtonFor(model => model.InsuranceSettled, 1) 

@Html.RadioButtonFor(model => model.InsuranceSettled, 0) 
2

試試這個:

@Html.RadioButton("InsuranceSettled", 1, Model.InsuranceSettled == 1) 
.... 
@Html.RadioButton("InsuranceSettled", 0, Model.InsuranceSettled == 0) 
0
@Html.RadioButtonFor(model => model.InsuranceSettled , "True", model.InsuranceSettled == 1? new { Checked = "checked" } : null) 

@Html.RadioButtonFor(model => model.InsuranceSettled , "False", model.InsuranceSettled == 0? new { Checked = "checked" } : null) 
相關問題