单选按钮不反映模型的价值 [英] Radio Button doesn't reflect Model's value
问题描述
我有一个班级的剑道网格,我为那个班级构建了一个编辑器模板来为其中一个字段生成一个单选按钮.
I have a kendo Grid for a class, and for that class I've built an editor template to produce a radio button for one of the fields.
这个单选按钮不反映属性的值并且总是false
,虽然我已经检查了值,通过在表单上打印它,我确定它是true代码>.如果我为该字段设置了默认值,则单选按钮将反映该值,而不管该字段的实际值如何.
This radio button doesn't reflect propertie's value and is always false
, although I've checked the value, by printing it on the form, and I'm sure it's true
. If I set a default value for that field, the radio button will reflect that value, regardless of the real value of the field.
我应该注意到我正在使用客户端模板来显示该字段的文本,并且工作正常.
I should note that I'm using a client template to display a text for that field, and it works fine.
这是网格:
@(Html.Kendo().Grid<Class>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(x => x.BActive).ClientTemplate("#= BActive ? 'Open':'Close' #");
/// removed for brevity
})
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(ds => ds.Ajax()
.Model(model => {
model.Id(x => x.SWhCode);
model.Field(x => x.BActive).DefaultValue(true);
})
)
)
这是在 editorTemplate
中生成单选按钮的行:
And this is the lines that produce the radio button inside the editorTemplate
:
<label>
@Html.RadioButtonFor(x => x.BActive, true, new { @class = "radio-inline" }) Open
</label>
<label>
@Html.RadioButtonFor(x => x.BActive, false, new { @class = "radio-inline" }) Close
</label>
推荐答案
bool
type will be render in client like this
bool
type will be render in client like this
true => True
false => False
您是否尝试过检查以 HTML 呈现的元素?您将看到 bool
值转换为大写的 string
.为了克服这个问题,您也应该在单选按钮中更改 true
和 false
,类型为 string
.
Have you try to inspect your element rendered in HTML? You'll see that the bool
value transform into capitalize string
. To overcome this you should change true
and false
in radio button with type string
too.
你的视图代码应该是这样的
Your view code should be like this
<label>
@Html.RadioButtonFor(x => x.BActive, "true", new { @class = "radio-inline" }) Open
</label>
<label>
@Html.RadioButtonFor(x => x.BActive, "false", new { @class = "radio-inline" }) Close
</label>
这篇关于单选按钮不反映模型的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!