Asp.net mvc 剔除数据绑定后未设置MVC Radiobutton默认值
我使用MVC强类型视图生成两个单选按钮Asp.net mvc 剔除数据绑定后未设置MVC Radiobutton默认值,asp.net-mvc,knockout.js,radio-button,Asp.net Mvc,Knockout.js,Radio Button,我使用MVC强类型视图生成两个单选按钮 <div> @Html.LabelFor(model => model.Eligible) @Html.RadioButtonFor(model => model.Eligible,"true", new { id="yes", data_bind = "checked: completeVM.Eligible " }) @Html.Label("yes", "YES")
<div>
@Html.LabelFor(model => model.Eligible)
@Html.RadioButtonFor(model => model.Eligible,"true", new { id="yes", data_bind = "checked: completeVM.Eligible " })
@Html.Label("yes", "YES")
@Html.RadioButtonFor(model => model.Eligible, "false", new {id="no", data_bind = "checked: completeVM.Eligible " })
@Html.Label("no", "No")
</div>
@LabelFor(model=>model.qualified)
@RadioButton(model=>model.qualified,“true”,new{id=“yes”,data\u bind=“checked:completeVM.qualified”})
@标签(“是”、“是”)
@RadioButton(model=>model.qualified,“false”,new{id=“no”,data\u bind=“checked:completeVM.qualified”})
@标签(“否”、“否”)
如果我不使用数据绑定,当页面加载时,默认情况下会设置“否”单选按钮。但是在我添加数据绑定后,两个单选按钮都没有被选中。我注意到在我的视图模型中,合格的显示为:“合格”:false;如果我选中“否”按钮,它将显示为“合格”:“false”。如何修复此问题,以便在加载页面时,默认情况下选中“否”按钮。谢谢这是一个常见问题。单选按钮值始终是字符串。然而,您需要布尔行为。最简单的修复方法是使用一个额外的计算可观测值为您进行转换: 假设“this”指向您的viewmodel:
this.EligibleRadioHelper = ko.computed({
read: function () {
return this.Eligible().toString();
},
write: function (value) {
this.Eligible(value === 'true');
},
owner: this
});
绑定此新帮助程序:
data-bind="checked: completeVM.EligibleRadioHelper"
当您想要布尔值(用于逻辑或持久化)时,请使用原始的observable。这是一个常见问题。单选按钮值始终是字符串。然而,您需要布尔行为。最简单的修复方法是使用一个额外的计算可观测值为您进行转换: 假设“this”指向您的viewmodel:
this.EligibleRadioHelper = ko.computed({
read: function () {
return this.Eligible().toString();
},
write: function (value) {
this.Eligible(value === 'true');
},
owner: this
});
绑定此新帮助程序:
data-bind="checked: completeVM.EligibleRadioHelper"
当您想要布尔值(用于逻辑或持久化)时,请使用原始的observable。您也可以尝试使用敲除3中提供的
checkedValue
绑定,而无需为视图模型编写计算函数。
您的代码将如下所示:
<div>
@Html.LabelFor(model => model.Eligible)
@Html.RadioButtonFor(model => model.Eligible,"true", new { id="yes", data_bind = "checked: completeVM.Eligible, checkedValue: true" })
@Html.Label("yes", "YES")
@Html.RadioButtonFor(model => model.Eligible, "false", new {id="no", data_bind = "checked: completeVM.Eligible, checkedValue: false " })
@Html.Label("no", "No")
@LabelFor(model=>model.qualified)
@RadioButton(model=>model.qualified,“true”,new{id=“yes”,data\u bind=“checked:completeVM.qualified,checkedValue:true”})
@标签(“是”、“是”)
@RadioButton(model=>model.qualified,“false”,new{id=“no”,data_bind=“checked:completeVM.qualified,checkedValue:false”})
@标签(“否”、“否”)
您也可以尝试使用敲除3中提供的
checkedValue
绑定,而无需为视图模型编写计算函数。
您的代码将如下所示:
<div>
@Html.LabelFor(model => model.Eligible)
@Html.RadioButtonFor(model => model.Eligible,"true", new { id="yes", data_bind = "checked: completeVM.Eligible, checkedValue: true" })
@Html.Label("yes", "YES")
@Html.RadioButtonFor(model => model.Eligible, "false", new {id="no", data_bind = "checked: completeVM.Eligible, checkedValue: false " })
@Html.Label("no", "No")
@LabelFor(model=>model.qualified)
@RadioButton(model=>model.qualified,“true”,new{id=“yes”,data\u bind=“checked:completeVM.qualified,checkedValue:true”})
@标签(“是”、“是”)
@RadioButton(model=>model.qualified,“false”,new{id=“no”,data_bind=“checked:completeVM.qualified,checkedValue:false”})
@标签(“否”、“否”)
谢谢您的快速回答。工作完美:)谢谢你的快速回答。工作完美:)