2014-02-27 52 views
4

在ASP.NET MVC4的Razor視圖,如何確保沒有一個單選按鈕默認爲選中的? MVC似乎爲我檢查了一個,即使我沒有聲明在下面的代碼中檢查一個。@ Html.RadioButtonFor默認爲未選中

@Html.RadioButtonFor(model => model.test, true, new { id = "radio_test_True"}) Yes 
@Html.RadioButtonFor(model => model.test, false, new { id = "radio_test_False"}) No 

感謝

回答

6

設置Model.test在您的控制器或模型的構造函數爲空,並確保它是空。 A bool必須是真或假,null將意味着既不被檢查。

public class WhateverTheModelIs 
{ 
    public bool? test { get; set; } 

    public WhateverTheModelIs() 
    { 
     test = null; 
    } 
} 

注:值設置爲null是多餘的,因爲它是默認的。爲了這個例子我想表達清楚。

+0

謝謝,使測試可以爲空是關鍵。 – Don