单选按钮不反映模型的值 [英] Radio Button doesn't reflect Model's value

查看:115
本文介绍了单选按钮不反映模型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个班级创建了一个Kendo Grid,并且针对该班级,我构建了一个编辑器模板来为其中一个字段生成单选按钮.

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类型将像这样在客户端中呈现

bool type will be render in client like this

true => True
false => False

您是否尝试检查以HTML呈现的元素?您会看到bool值转换为大写的string.为了解决这个问题,您还应该在单选按钮中同时将truefalse更改为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.

您的查看代码应为这样

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆