我正在使用Knockout和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>
...
我的知道:
...
self.isActive = ko.observable(product.isActive);
...
正确更新数据库,但在加载页面时未显示任何单选按钮。我也尝试使用checked =“checked”html属性,它也不起作用。有什么建议吗?
答案 0 :(得分: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>
编写observable以将isActive中的布尔值用作字符串。
self.isActive = ko.computed(function() {
return product.isActive.toString();
});