2013-05-02 40 views
0

我使用淘汰賽與MVC 4.我CSHTML是:單選按鈕沒有顯示爲選中

... 
<span>@Html.RadioButtonFor(m => m.isActive, true, new { @class = "statusRadioButton", data_bind = "checked: isActive" })</span> 
<span>@Html.RadioButtonFor(m => m.isActive, false, new { @class = "statusRadioButton", data_bind = "checked: isActive" })</span> 
... 

我KO:

... 
self.isActive = ko.observable(product.isActive); 
... 

更新數據庫中正確,但它在頁面加載時不顯示任何單選按鈕。我也嘗試使用checked =「checked」html屬性,它也不起作用。有什麼建議?

+0

是'product.isActive'可觀察的嗎? – Thewads 2013-05-02 14:19:06

回答

1

您的product.isActive是否可觀察?如果是這樣,那麼你將需要執行觀察像product.isActive()

通過初始化像self.isActive = ko.observable(product.isActive());你只是要設置一次。

嘗試打開它來觀察的,如:

self.isActive = ko.computed(function() { 
           return product.isActive(); 
          }); 

編輯: 試着改變你的單選按鈕,這樣的:

<span>@Html.RadioButtonFor(m => m.isActive, true, new { @class = "statusRadioButton", data_bind = "checked: isActive", value="true" })</span> 
<span>@Html.RadioButtonFor(m => m.isActive, false, new { @class = "statusRadioButton", data_bind = "checked: isActive", value="false" })</span> 

和寫作你觀察到的isActive使用布爾值作爲字符串。

self.isActive = ko.computed(function() { 
           return product.isActive.toString(); 
          }); 
+0

我試過了,它會拋出錯誤,我認爲這是因爲isActive是一個布爾值,而不是函數。所以我改變了你的代碼來返回product.isActive; ,現在不會中斷,但問題仍然存在。 – 2013-05-02 14:26:01

+0

from KO docs:'對於單選按鈕,當且僅當參數值等於單選按鈕節點的值屬性時,KO纔會設置要檢查的元素。他們的工作方式與複選框不同。看到我的編輯 – Thewads 2013-05-02 14:34:20

+0

還是什麼都沒有。我嘗試了一切。仍然更新數據庫,但在頁面加載時不顯示已檢查的單選按鈕。 – 2013-05-02 15:18:03